我运行这段代码
#Gets to the Calendar for the week with the Login Details
calendar = driver.get('https://www.investing.com/economic-calendar/')
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[9]/div[3]')
driver.find_element_by_xpath('//*[@id="onetrust-accept-btn-handler"]').click()
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[10]/div')
driver.find_element_by_xpath('/html/body/div[10]/div/div[4]/button[1]').click()
有时候我成功有时会出错
ElementClickInterceptedException: element click intercepted: Element <a onclick="overlay.overlayLogin();" href="javascript:void(0);" class="login bold">...</a> is not clickable at point (821, 19). Other element would receive the click: <div class="allow-notifications-popup-wrapper shown">...</div>
(Session info: chrome=84.0.4147.135)
因为顽皮的弹出窗口/警报并不总是出现。
如何编写IF(弹出窗口)然后单击它
OR
如果弹出不跳以下两行?
谢谢
答案 0 :(得分:2)
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
应该能够完全绕开弹出窗口。
答案 1 :(得分:1)
也许有很多方法可以解决您的问题,但是由于您基本上是 试图避免导致这种确切错误的情况,我认为使用TRY / EXCEPT将是最简单的:
try:
driver.find_element_by_xpath('/html/body/div[10]/div/div[4]/button[1]').click()
except ElementClickInterceptedException:
pass
它能解决您的问题吗?
答案 2 :(得分:1)
JavaScript单击将起作用,但是绕过了可能是潜在错误的实际原因。
ElementClickInterceptedException: element click intercepted:
由于多种原因,可能会发生此异常。根据硒文档,上述异常的原因是:
表示无法正确执行点击,因为 目标元素以某种方式被遮盖。
要解决此问题,您可以使用webdriverwait,然后检查元素是否可单击,然后单击它
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "YOURXPATH']"))).click()