Home > database >  how to set common time sleep for exporting all data in different webpages in selenium python
how to set common time sleep for exporting all data in different webpages in selenium python

Time:01-24

i Am doing UI automation in multiple webpages, handled all webpages in single script, My problem is in a webpage there is 100 data, to export that it will take 10 sec, in another webpage that contains 500 data that will take 50 sec to export.

``

def Export():
global status,driver,wait
#driver.implicitly_wait(300)
try:
    driver.find_element(By.XPATH, "//button[contains(@name,'confirm')]").click()
    print("Export Button Clicked")
    time.sleep(50)
except:
    status="fail"
    print("Export Button Not Displayed")

Export():

I am expecting to set common time sleep for all webpages to export data

How to handle it?

CodePudding user response:

set a timeout for the entire script and adjust it according to the maximum time it should take to export the data

import time

timeout = 60 # seconds

def export():
    try:
        driver.find_element(By.XPATH, "//button[contains(@name,'confirm')]").click()
        print("Export Button Clicked")
        
        time.sleep(timeout)
    except:
        status="fail"
        print("Export Button Not Displayed")
export()

or you can use an explicit wait function

def export():
    try:
        driver.find_element(By.XPATH, "//button[contains(@name,'confirm')]").click()
        print("Export Button Clicked")
        
        # Wait for export to finish before proceeding
        export_complete = EC.presence_of_element_located((By.XPATH, "//export-complete"))
        WebDriverWait(driver, 60).until(export_complete)
    except:
        status="fail"
        print("Export Button Not Displayed")

export()

CodePudding user response:

  1. You should never use hardcoded pauses.
    You need to get indication data is exported. It can be done according to some expected changes on web page or maybe file is downloaded and exists on the disk etc.
  2. In case there is no way to avoid this and you can only use hardcoded pauses you can set some kind of dictionary of delay values per web site so you will be able to get the delay value (value) per link (key).
  • Related