org.openqa.selenium.ElementClickInterceptedException:在无头模式下使用Selenium和Java元素单击被拦截的错误 [英] org.openqa.selenium.ElementClickInterceptedException: element click intercepted error using Selenium and Java in headless mode
问题描述
我有一个正在使用Java和Selenium的项目.在UI模式下测试工作正常.但是在无头模式下,我会收到此错误
I have a project that I am working on with java and selenium. the test work OK in UI mode. However in headless mode I get this error
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模式下工作).这是我的代码
how can I resolve this issue (working in UI mode). this is my code
WebDriver driver = getWebDriver();
WebElement element;
Thread.sleep(60000);
element = driver.findElement(By.xpath("//label[@formcontrolname='reportingDealPermission']"));
element.click();
为什么硒中没有移动到元素并破坏所有层的操作.这是用户界面.这是在UI模式下工作的,而不是在无头模式下工作,睡眠了6分钟并且无法解决,所以这不是时间问题
why in selenium there is no operation to move to the element and break all layers. this is the UI. this is working in UI mode not working in headless mode, made sleep for 6 minutes and not resolved so this is not time issue
推荐答案
此错误消息...
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 被其他某个元素拦截了.
...implies that the click on the desired element was intercepted by some other element.
理想情况下,在对任何元素调用 click()
时,您需要诱导定位器策略:
Ideally, while invoking click()
on any element you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following 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 后,如果仍然无法正常工作并且仍然出现异常,则还可以考虑以下几种其他措施:
After changing to headless if it still doesn't works and still get exception there still a couple of other measures to consider as follows:
-
视口:
Chrome browser in Headless mode doesn't opens in maximized mode. So you have to use either of the following commands/arguments to maximize the headless browser 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));
-
Additionally, you can also wait for the intercept element to be invisible using the ExpectedConditions
invisibilityOfElementLocated
before attempting theclick()
as follows:-
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();
您可以在以下位置找到几个相关的相关讨论:
You can find a couple of related relevant discussions in:
这篇关于org.openqa.selenium.ElementClickInterceptedException:在无头模式下使用Selenium和Java元素单击被拦截的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!