[英]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.