有很多方法可以等待一个对象,例如在 selenium python 中可点击的按钮。我使用 time.sleep()
和/或 WebDriverWait...until
,它工作正常。
但是,当有数百个对象时,有没有办法在全局范围内设置默认时间延迟,而不是在每个对象上实现它?
click()
Action 应该有条件等待时间?
最佳答案
我想出了这个:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def myClick(by, desc):
wait = WebDriverWait(driver, 10)
by = by.upper()
if by == 'XPATH':
wait.until(EC.element_to_be_clickable((By.XPATH, desc))).click()
if by == 'ID':
wait.until(EC.element_to_be_clickable((By.ID, desc))).click()
if by == 'LINK_TEXT':
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, desc))).click()
有了这个函数,代码:
driver.find_element_by_link_text('Show Latest Permit').click()
将会
myClick('link_text', 'Show Latest Permit')
改为。
我已经运行了几个星期,点击了数百个元素,我再也没有看到错误。
关于python - 使用 python 和 Selenium 等待元素可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56380889/