3

Im trying to build little helper for my work, which simulates human behaviour on the website via selenium.

My code works just fine, but one element on the website takes around 90 seconds to load and always fails to load. All I need on that particular webpage is to click a button once it appears and move on.

Is there a way to skip waiting for full page to download? The rest of the page loads in like 2-5 seconds, but this one bloody element...

Here is my code without any 'waits' applied

driver.get('https://power.dat.com/') post_trucks_button =
driver.find_element_by_xpath('/html/body/nav[2]/div[1]/div[2]/a[1]')
post_trucks_button.click()

And here is some things I tried, but none seemed to work:

###
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="main"]/div/section[1]/div')))
###
try:
   element = WebDriverWait(driver, 10).until(
   EC.presence_of_element_located((By.XPATH, '//*[@id="main"]/div/section[1]/div')))
finally:
   post_trucks_button = driver.find_element_by_xpath('/html/body/nav[2]/div[1]/div[2]/a[1]')
   post_trucks_button.click()
###
wait = WebDriverWait(driver, 10)
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, '//[@id="main"]/div/section[1]/div')))
ActionChains(driver).move_to_element(men_menu).perform()
post_trucks_button = driver.find_element_by_xpath('/html/body/nav[2]/div[1]/div[2]/a[1]')
###
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[NoSuchElementException])element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="main"]/div/section[1]/div')))
post_trucks_button = driver.find_element_by_xpath('/html/body/nav[2]/div[1]/div[2]/a[1]')
post_trucks_button.click()
###
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="main"]/div/section[1]/div')))
post_trucks_button = driver.find_element_by_xpath('/html/body/nav[2]/div[1]/div[2]/a[1]')
post_trucks_button.click()
###

P.S. all required modules are imported, just didnt include them above

P.S.S. I kinda have no idea what im doing, just started learning python

2 Answers2

2

A bit of more information with respect to:

  • The relevant HTML of the button you want to click.
  • Error encountered while clicking the button.
  • The other element which takes around 90 seconds to load and the relevant HTML of the WebElement
  • How you concluded the said element takes time.

would have helped us in constructing a more canonical answer. Ideally, your Test Code / Code Block should cater to your Test Aim / Test Steps leaving out the other aspects of the website and other elements.


Solution

As per best practices, while invoking click() on any WebElement you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click()
    
  • Note : You have to add the following imports :

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

You can find a relevant detailed discussion in How to click on a element through Selenium Python


This usecase

However, there is a provision to stop the loading of the Web Page once your desired element is clickable/interactable by invoking window.stop() as follows:

post_trucks_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_identifies")))
driver.execute_script("window.stop();")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

You said the element often takes around 90 seconds to load, why did you just add 10 second to timeout in Wait? IMHO, it would be 90 seconds.

This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds

See more: https://selenium-python.readthedocs.io/waits.html#explicit-waits

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20