How to combine execute_scipt and WebdriverWait

别说谁变了你拦得住时间么 提交于 2021-02-08 07:42:38

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/57747480/how-to-combine-execute-scipt-and-webdriverwait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!