在Firefox浏览器上获取ElementClickIntercepted异常,因为弹出窗口会干扰Chrome正常工作

问题描述

1.Newsletter弹出窗口显示在我的网站上。

2。此新闻通讯弹出模式可以在3分钟内显示在任何页面上,所以我无法计时。

  1. 这使我的自动化脚本(使用geb-spock)失败。

  2. 为了现在显示时事通讯弹出模式,我在下面添加一个cookie。

driver.manage().addCookie(new Cookie("signupNoThanks","optednoThanks"));

错误消息

 `selenium.ElementClickInterceptedException` error i.e `Element <element1> is not clickable at point (1085,112) because another element <Newletter pop up modal> obscures it`

我尝试打印cookie,并且可以在日志中看到

[![Cookies log ss] [1]] [1]

谁能在Firefox浏览器上指出问题所在,我添加的cookie在chrome上可以很好地工作,但有时在firefox上无法通过一些自动测试用例。 Firefox是否由于某些安全原因阻止了我们? [1]:https://i.stack.imgur.com/lS1r7.png

解决方法

该错误似乎确实是cookie所特有的,因为您可以在日志中看到这些cookie。错误“元素不可点击”是一个通用错误,您可以在Selenium Web Driver & Java. Element is not clickable at point (x,y). Other element would receive the click

上阅读有关此错误的更多信息

尝试以下方法解决此错误

  • 尝试使用其他选择器(例如XPath)来定位元素。
  • 该元素在视口中不可见,您能否在单击该元素之前尝试滚动到该元素?
  • 使用Actions类执行点击。
,

您可以为整个项目中使用的点击创建扩展方法,并将其包装在try catch中以捕获元素拦截的异常。然后,您可以接受弹出窗口,然后继续正常运行而无需担心。

(下面的代码是c#):

public static class SeleniumExtensions
{
   public static void ClickElement(this IWebElement element)
   {
      try
        {
          element.Click();
        } catch (ElementClickInterceptedException ex)
        {
          Console.WriteLine("Click was intercepted,attempting to dismiss pop up: ",ex);
          //Code to click or dismiss your popup goes here

          //retry click
          element.Click();
        }
    }
}

然后在整个项目中,而不是使用默认的硒,请单击use:

element.ClickElement();