zlee11
zlee11

Reputation: 69

Time.sleep() on ChromeDriver

I am using ChromeDriver for some web scraping using Python. My code uses browser.find_element_by_xpath but I have to include time.sleep(3) between clicks/input because I need to wait for the webpage to load before I can execute the next line of code.

Was wondering if anyone knows the best way to do this? Perhaps a feature that can automatically execute the next line instantly when the browser loads instead of waiting for an arbitrary number of seconds?

Thanks!

Upvotes: 1

Views: 2268

Answers (2)

EnriqueBet
EnriqueBet

Reputation: 1473

I have worked with a function for this cases that add robustness to the script. For instance for finding an element by xpath:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions as EC


def findXpath(xpath,driver):
    actionDone = False
    count = 0
    while not actionDone:
        if count == 3:
            raise Exception("Cannot found element %s after retrying 3 times.\n"%xpath)
            break
        try:
            element = WebDriverWait(driver, waitTime).until(
                    EC.presence_of_element_located((By.XPATH, xpath)))
            actionDone = True
        except:
            count += 1
    sleep(random.randint(1,5)*0.1)
    return element 

Let me know of this works for you!

Upvotes: 0

supputuri
supputuri

Reputation: 14135

Try with explicit wait using expected_conditions as shown below.

Imports need:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Then you can wait for the element to be present before interacting.

# waiting for max of 30 seconds, if element present before that it will go on to the next line.
ele = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"xpath_goes_here")))
ele.click() # or what ever the operation like .send_keys()

This way the application will dynamically wait until the element is present. Update the time from 30 seconds if required based on your application.

Also you can use different location strategies when checking for the element presence eg: By.CSS_SELECTOR/By.ID/By.CLASS_NAME

Upvotes: 4

Related Questions