仅在启用无头模式时,我的脚本才会失败。请建议我在编码时应该注意的方式。
以下是我启用无头时收到的一些例外情况,
1. WebDriverException: unknown error: Element <input type="radio" class="wizard-input" name="5a68a4c173bb-TOTAL-1" id="5a68a4c173bb-TOTAL-1" value="1"> is not clickable at point (496, 551). Other element would receive the click: <div class="navigation-bar">...</div>
尝试应用等待条件甚至滚动并单击。
2. TimeoutException: Expected condition failed: waiting for element to be clickable: By.cssSelector: div.icon.icon-add.add-contact-button (tried for 50 second(s) with 500 MILLISECONDS interval)
试图应用Marcel建议的条件。据说它甚至超过了100秒
以下是我的代码的几个例子,
public void clickForwardButton(){
WaitTillElementToBeClickable("xpath", LoginData.Forward);
ScrollAndClickOnElement("xpath", LoginData.Forward);
} //The error seems to be like it wont scroll properly and hence I receive element not found exception
protected void WaitTillElementToBeClickable(String locatorType, String locatorValue) {
try {
WebDriverWait wait = new WebDriverWait(driver, TIME_OUT_IN_SECONDS);
if (locatorType.equalsIgnoreCase("cssSelector")) {
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locatorValue)));
} else if (locatorType.equalsIgnoreCase("xpath")) {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(locatorValue)));
} else if (locatorType.equalsIgnoreCase("id")) {
wait.until(ExpectedConditions.elementToBeClickable(By.id(locatorValue)));
}
} catch (Exception e) {
logger.error("Webdriver Locator Error" + e);
}
}
答案 0 :(得分:1)
如果您没有使用WebDriverWait,请尝试一下
int seconds = 5;
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourID")));
您必须导入OpenQA.Selenium.Support.UI
才能使用WebDriverWait
。
修改
由于WebDriverWait
方法未提供解决方案,因此请尝试向ChromeOptions
添加其他参数以设置窗口大小。由于默认的无头窗口大小可能比非无头窗口大小小很多,所以值得一试。设置更大窗口大小的额外好处是减少了滚动的需要。
ChromeOptions options = new ChromeOptions();
options.addArgument("headless");
options.addArgument("window-size=1920,1080");
// or
options.addArguments("headless", "window-size=1920,1080");