Python Selenium中的IF/THEN语句
我运行这个代码Python Selenium中的IF/THEN语句,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我运行这个代码 #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=&
#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)
element单击拦截异常:element单击拦截:element在点(821,19)处不可单击。其他元素将收到单击:。。。
(会话信息:chrome=84.0.4147.135)
因为调皮的弹出/警报并不总是出现
我如何写一个IF(弹出窗口在那里),然后单击它
或
如果没有弹出,请跳过以下两行
谢谢也许有很多方法可以解决你的问题,但既然你基本上 为了避免出现导致此错误的情况,我认为使用TRY/EXCEPT是最简单的方法:
try:
driver.find_element_by_xpath('/html/body/div[10]/div/div[4]/button[1]').click()
except ElementClickInterceptedException:
pass
它能解决你的问题吗
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
应该能够完全绕过弹出窗口。JavaScript单击将起作用,但它绕过了可能是潜在错误的实际原因 此异常可能由于多种原因而发生。根据selenium docs,上述异常的原因是: 指示无法正确执行单击,因为 目标元素以某种方式被遮住了 要解决此问题,可以使用webdriverwait,然后检查元素是否可单击,然后单击它
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "YOURXPATH']"))).click()
请参阅您是否为系统编写了自动化测试?我需要上面的自动化来进入页面,然后用BS刮取,谢谢页面没有预定义的URI吗?如果没有,我会考虑重试功能的解决方案,所以定义例如最大5次尝试,当您捕获异常时,从一开始就尝试这个过程。但问题的根本原因是您无法控制自动化的系统。如果在第一步出现弹出窗口,将无助于解决问题。
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "YOURXPATH']"))).click()