简体   繁体   English

如果检查元素点击的语句不会被拦截(Python Selenium)

[英]If statement to check element click won't be intercepted (Python Selenium)

My code waits for an element to be clickable, but even when this occurs there is still a popup box above it that remains for a few seconds while a table is populated.我的代码等待一个元素可点击,但即使发生这种情况,它上面仍然有一个弹出框,在填充表格时会保留几秒钟。 This means my click is getting intercepted.这意味着我的点击被拦截了。

Is there a way to run an if statement to say 'if click will be intercepted, time.sleep(1) then try again?有没有办法运行 if 语句来说“如果点击将被拦截, time.sleep(1)然后再试一次? I don't really want to use a simple time.sleep(3) on its own as the time to load will vary and I'd like to be efficient.我真的不想单独使用一个简单的time.sleep(3) ,因为加载时间会有所不同,我想提高效率。

Current code is just the initial step:当前代码只是第一步:

element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "location-expandable-click")))
element.click()

Best is to wait until popup box will disappear.最好是等到弹出框消失。 You can use wait with invisibility_of_element_located for that.为此,您可以使用带有invisibility_of_element_located等待。

Second option is to use JavaScript to click:第二种选择是使用 JavaScript 来点击:

driver.execute_script("arguments[0].click();", WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "location-expandable-click"))))

Third, use Try Except :第三,使用Try except

element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "location-expandable-click")))
clicked = False
for _ in range(5):
    try:
        element.click()
        clicked = True
        break
    except ElementClickInterceptedException as e:
        print("Failed to click, will try again after 1 second")
        time.sleep(1)

if not clicked:
    print("Failed to click after 5 tries")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium:元素点击拦截: - python selenium: element click intercepted: Python Selenium 元素点击拦截 - Python Selenium element click intercepted 元素点击截获硒 - Element click intercepted selenium 元素点击被拦截(点击工作完美)(硒python)(弹出) - element click intercepted (click work perfectly)(selenium python)(popup) ElementClickInterceptedException: 消息: 元素点击被拦截: <label>Selenium 和 Python 无法点击</label>元素 - ElementClickInterceptedException: Message: element click intercepted: Element <label> is not clickable with Selenium and Python Python & Selenium:ElementClickInterceptedException:消息:元素点击拦截错误 - Python & Selenium: ElementClickInterceptedException: Message: element click intercepted error Selenium - 元素点击被拦截:元素在点不可点击 - Selenium - element click intercepted: Element is not clickable at point 硒元素不会点击 - Selenium element won't click selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素不可点击 Selenium 和 Python - selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python ElementClickInterceptedException: 元素点击被拦截。 其他元素会收到点击:Selenium Python - ElementClickInterceptedException: element click intercepted. Other element would receive the click: Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM