python - Selenium 中的 IF/THEN 语句

标签 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="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(弹出窗口)然后单击它

或者

IF pop-不存在跳过以下两行吗?

谢谢

最佳答案

WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

应该能够完全绕过弹出窗口。

关于python - Selenium 中的 IF/THEN 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63550722/

相关文章:

python - 如何使用python统计一个字母在word中出现的次数

python - Keras神经网络: ValueError - input shape is wrong

python - httplib.BadStatusLine : '' on Linux but not Mac

python - 集成到 for 循环 python 中时,actionchains send_keys 发送的重复文本

ruby-on-rails - capybara , Selenium 与隐藏或显示交互 : none css properties

javascript - WebStorm/ cucumber : Undefined step reference

javascript - Protractor 关闭当前选项卡

python - 更改 matplotlib.slider 标签文本大小

python - 更好的 Pandas 方法来计算不同列中值的频率

html - 为 selenium 测试寻找 CSS 选择器