我正在尝试在网页上执行正文点击。 当我加载网页时,会显示一个弹出窗口。我可以通过单击屏幕上的任意位置来关闭该弹出窗口。这个弹窗没有标准化的类名或 id(不断变化)所以我想模拟一个按钮点击 body 来关闭它。
我试过了
driver.find_element_by_tag_name('html').click()
driver.find_element_by_tag_name('body').click()
我没有收到错误,但弹出窗口没有关闭
然后我尝试点击一个随机的背景元素,但是当我尝试时它给了我一个错误
driver.find_element_class_name('abc').click()
ElementClickInterceptedException: Message: element click intercepted
有什么办法可以点击body或者其他html标签来关闭它吗?
<div tabindex="-1" aria-modal="true" role="alertdialog" class="ca ew bz bx by c0 sb cr fs cw ap st t6 t7 sv sq i9 sw sx sy">
<div data-baseweb="block" class="qv c4 db c3">
<div data-baseweb="block" class="aq ar fq fr">A message </div>
<div data-baseweb="block" class="f5 aq e5 d8 at ao"><p data-baseweb="typo-paragraphlarge" class="fo aq e5 d8 at au dg">Spend $50 </p></div></div></div>
答案 0 :(得分:0)
为该弹出窗口使用 dom 元素,并在大约 2-3 秒的短暂等待后单击该元素。它应该可以工作。
注意:这里您使用了 driver.find_element_name,但它应该是 dom 元素,如 id、类或与该 dom 元素相关的其他属性。
driver.find_element_name('abc').click()
ElementClickInterceptedException: Message: element click intercepted
答案 1 :(得分:0)
只是弹出窗口需要一些时间才能显示
driver.find_element_by_tag_name('html').click()
已执行..
尝试在执行body.click()
之前睡一会
这是一个例子,但它是在java中
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://bharathish-diggavi.github.io/Selenium-Practice/popup.html");
driver.findElement(By.id("popupButton")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.tagName("body")).click();;
代码没有睡眠就无法工作。
答案 2 :(得分:0)
您不能点击 body
或带有 Selenium
的某些背景元素,而这些元素实际上被其他元素覆盖,因为 Selenium
模拟用户的 UI
操作。
因此,如果您尝试单击下面的元素,则上面的元素实际上是单击的收据。这就是 element click intercepted
错误的字面意思。
在这种情况下,您可以使用 JavaScript
点击。
因此,要单击带有 body
的 JS
,请尝试以下操作:
driver.execute_script("document.getElementByTagName('body').click()")