元素不可点击……使用无头浏览时出错
Element is not clickable at point ... error when using headless browsing
当我在无头模式下 运行 下面的代码段时,出现此错误“org.openqa.selenium.ElementClickInterceptedException:元素点击被截获:元素在点 (209, 760) 不可点击”。当浏览器显示为 运行 时,我没有错误并且测试正常通过。正如您在下面看到的,我尝试等待,js 执行器,动作移动到元素,但仍然没有好的结果。我正在使用 xpath 来定位/定义元素,而不是坐标。请问为什么会这样,我该如何解决?提前致谢。
@Test(priority = 1)
public void verifyAddUserWithMarkedMandatoryFields() {
// accessing add user webpage / functionality
userListObject.getAddUserButton().click();
// inserting data to complete form
addOrEditUserPageObject.insertCredentials(userModel.getUsername(), userModel.getEmail(), "", userModel.getPassword());
// clicking Submit when becoming enabled
WebDriverWait myWaitVariable = new WebDriverWait(driver, 5);
myWaitVariable.until(ExpectedConditions.elementToBeClickable(addOrEditUserPageObject.getSubmitButtonAddOrEdit()));
// Actions actions = new Actions(driver);
// actions.moveToElement(addOrEditUserPageObject.getSubmitButtonAddOrEdit()).click().perform();
JavascriptExecutor jse = (JavascriptExecutor)driver;
// jse.executeScript("scroll(209, 760)"); // if the element is on top.
jse.executeScript("scroll(760, 209)"); // if the element is on bottom.
addOrEditUserPageObject.getSubmitButtonAddOrEdit().click();
}
您应该为无头模式添加屏幕尺寸,如下所示:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
我在这里放了很多东西,这里唯一相关的一点是"--window-size=1920,1080"
,这应该可以解决你的问题。
剩下的就是展示如何管理事物,包括无头模式的其他相关设置。
当我在无头模式下 运行 下面的代码段时,出现此错误“org.openqa.selenium.ElementClickInterceptedException:元素点击被截获:元素在点 (209, 760) 不可点击”。当浏览器显示为 运行 时,我没有错误并且测试正常通过。正如您在下面看到的,我尝试等待,js 执行器,动作移动到元素,但仍然没有好的结果。我正在使用 xpath 来定位/定义元素,而不是坐标。请问为什么会这样,我该如何解决?提前致谢。
@Test(priority = 1)
public void verifyAddUserWithMarkedMandatoryFields() {
// accessing add user webpage / functionality
userListObject.getAddUserButton().click();
// inserting data to complete form
addOrEditUserPageObject.insertCredentials(userModel.getUsername(), userModel.getEmail(), "", userModel.getPassword());
// clicking Submit when becoming enabled
WebDriverWait myWaitVariable = new WebDriverWait(driver, 5);
myWaitVariable.until(ExpectedConditions.elementToBeClickable(addOrEditUserPageObject.getSubmitButtonAddOrEdit()));
// Actions actions = new Actions(driver);
// actions.moveToElement(addOrEditUserPageObject.getSubmitButtonAddOrEdit()).click().perform();
JavascriptExecutor jse = (JavascriptExecutor)driver;
// jse.executeScript("scroll(209, 760)"); // if the element is on top.
jse.executeScript("scroll(760, 209)"); // if the element is on bottom.
addOrEditUserPageObject.getSubmitButtonAddOrEdit().click();
}
您应该为无头模式添加屏幕尺寸,如下所示:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
我在这里放了很多东西,这里唯一相关的一点是"--window-size=1920,1080"
,这应该可以解决你的问题。
剩下的就是展示如何管理事物,包括无头模式的其他相关设置。