我運行這段代碼#Gets to the Calendar for the week with the Login Detailscalendar = 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 pop-not有跳過下面兩行嗎?謝謝
3 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
應該能夠完全繞過彈出窗口。

胡說叔叔
TA貢獻1804條經驗 獲得超8個贊
JavaScript 點擊將起作用,但它繞過了可能是潛在錯誤的實際原因。
ElementClickInterceptedException:?element?click?intercepted:
由于多種原因,可能會發生此異常。根據 selenium 文檔,上述異常的原因是:
指示無法正確執行單擊,因為目標元素以某種方式被遮擋。
要解決此問題,您可以使用 webdriverwait 然后檢查元素是否可單擊,然后單擊它
WebDriverWait(driver,?10).until(EC.element_to_be_clickable((By.XPATH,?"YOURXPATH']"))).click()

Helenr
TA貢獻1780條經驗 獲得超4個贊
可能有很多方法可以解決您的問題,但是由于您基本上是在嘗試避免導致這種確切錯誤的情況,所以我認為使用 TRY/EXCEPT 是最簡單的方法:
try:
driver.find_element_by_xpath('/html/body/div[10]/div/div[4]/button[1]').click()
except ElementClickInterceptedException:
pass
它能解決你的問題嗎?
添加回答
舉報
0/150
提交
取消