2

I wonder if there is a way to combine execute_script() and WebdriverWait. Currently I have the following code:

network_list = driver.find_element_by_xpath('//*[@id="folder_box"]/div[1]/div/div[2]/div[1]')
wait = WebDriverWait(driver, 4)
try:
    wait_network_list = wait.until(EC.element_to_be_clickable((By.XPATH, 'network_list')))
except:
    driver.execute_script("arguments[0].click();", network_list)

The code does what it's supposed to do, but I guess this is a ugly way. Is there a way to combine my try and except statement to one line of code?

dws
  • 53
  • 4

1 Answers1

2

You can invoke WebdriverWait within execute_script() method as follows:

try:
    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='folder_box']/div[1]/div/div[2]/div[1]"))))
    print("Element was clicked")
except TimeoutException:
    print("Element wasn't clicked")
    break
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352