org.openqa.selenium.ElementClickInterceptedException:在无头模式下使用Selenium和Java元素单击被拦截的错误

时间:2020-06-08 10:58:43

标签: java selenium xpath css-selectors webdriverwait

我有一个正在使用Java和Selenium的项目。 在UI模式下测试工作正常。 但是在无头模式下,我会收到此错误

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>

如何解决此问题(在UI模式下工作)。这是我的代码

WebDriver driver = getWebDriver();
        WebElement element;
        Thread.sleep(60000);
        element = driver.findElement(By.xpath("//label[@formcontrolname='reportingDealPermission']"));
        element.click();

为什么硒中没有移动到元素并破坏所有层的操作。 这是UI。 这是在UI模式下工作,而不是在无头模式下工作,睡眠了6分钟并且无法解决,所以这不是时间问题

enter image description here

2 个答案:

答案 0 :(得分:2)

尝试添加明确的等待时间

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission']"))).click();

如果这不起作用,请尝试使用JS执行器

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission']"))); 
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

答案 1 :(得分:2)

此错误消息...

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>

...表示在所需元素上的 click 被其他某个元素拦截了。


点击元素

理想情况下,在对任何元素调用click()时,您需要为elementToBeClickable()引入WebDriverWait,并且可以使用以下Locator Strategies之一:

  • cssSelector

    new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
    
  • xpath

    new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission' and @ng-reflect-name='reportingDealPermission']"))).click();
    

更新

更改为 headless 后,如果仍然无法正常工作并且仍然出现异常,则还有其他一些措施可供考虑,如下所示:

  • Chrome browser in Headless mode不会以最大化模式打开。因此,您必须使用以下命令/参数之一来最大化无头浏览器Viewport

    • 添加参数start-maximized

      ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless");
      options.addArguments("start-maximized");
      WebDriver driver = new ChromeDriver(options);
      
    • 添加参数--window-size

      ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless");
      options.addArguments("--window-size=1400,600");
      WebDriver driver = new ChromeDriver(options);
      
    • 使用setSize()

      ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless");
      WebDriver driver = new ChromeDriver(options);
      driver.manage().window().setSize(new Dimension(1440, 900));
      

您可以在Not able to maximize Chrome Window in headless mode

中找到详细的讨论
  • 此外,您还可以使用ExpectedConditions invisibilityOfElementLocated等待 intercept元素不可见 ,然后尝试进行click(),如下所示:< / p>

    • cssSelector

      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.footer")));
      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
      
    • xpath

      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='footer']")));
      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission' and @ng-reflect-name='reportingDealPermission']"))).click();
      

参考文献

您可以在以下位置找到一些相关的相关讨论: