opi*_*ike 2 python selenium selenium-webdriver
我正在尝试使用 Expected_conditions.element_to_be_clickable 但它似乎不起作用。在大约 30% 的运行中,我仍然看到“元素...不可点击”错误。
这是完整的错误消息:
selenium.common.exceptions.WebDriverException:消息:未知错误:元素...在点(621、337)处不可单击。其他元素将收到点击:...(会话信息:chrome=60.0.3112.90)(驱动程序信息:chromedriver=2.26.436421(6c1a3ab469ad86fd49c8d97ede4a6b96a49ca5f6),platform=Mac OS X 10.12.6 x86_64)
这是我正在使用的代码:
def wait_for_element_to_be_clickable(selector, timeout=10):
global driver
wd_wait = WebDriverWait(driver, timeout)
wd_wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)),
'waiting for element to be clickable ' + selector)
print ('WAITING')
return driver.find_element_by_css_selector(selector)
Run Code Online (Sandbox Code Playgroud)
更新:
所以现在这真的很奇怪。即使我添加几个固定的等待时间,它仍然偶尔会抛出错误消息。这是进行调用的代码:
sleep(5)
elem = utils.wait_for_element_to_be_clickable('button.ant-btn-primary')
sleep(5)
elem.click()
Run Code Online (Sandbox Code Playgroud)
最终创建了我自己的自定义函数来处理异常并执行重试:
""" custom clickable wait function that relies on exceptions. """
def custom_wait_clickable_and_click(selector, attempts=5):
count = 0
while count < attempts:
try:
wait(1)
# This will throw an exception if it times out, which is what we want.
# We only want to start trying to click it once we've confirmed that
# selenium thinks it's visible and clickable.
elem = wait_for_element_to_be_clickable(selector)
elem.click()
return elem
except WebDriverException as e:
if ('is not clickable at point' in str(e)):
print('Retrying clicking on button.')
count = count + 1
else:
raise e
raise TimeoutException('custom_wait_clickable timed out')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8585 次 |
| 最近记录: |