通常出现在chrome驱动程序中的元素不可点击错误也恰好在firefox中显示。显示错误消息:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (141, 299.29998779296875). Other element would receive the click: <div class="showOnTop" id="loadingPanelContainer"></div>
Command duration or timeout: 209 milliseconds
Build info: version: '2.51.0', revision: '1af067dbcaedd7d2ab9af5151fc471d363d97193', time: '2016-02-05 11:20:57'
System info: host: 'Bhaveen-ThinkPad', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'i386', os.version: '3.13.0-77-generic', java.version: '1.7.0_95'
Session ID: 08e0d738-b946-4886-a179-9659d44b717b
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=44.0.2}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
at config.KeyActions.handleLeftMenu(KeyActions.java:479)
at scripts.Vital_Data_Script.setVitalData(Vital_Data_Script.java:383)
at scripts.Vital_Data_Script.executeActions(Vital_Data_Script.java:95)
at scripts.Vital_Data_Script.executeTestCase(Vital_Data_Script.java:60)
at scripts.Vital_Data_Script.main(Vital_Data_Script.java:31)
答案 0 :(得分:2)
您应该等待元素可点击,您可以使用:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.<your locator>));
或者有时你甚至需要将鼠标悬停在元素上以使其可点击。你可以这样做:
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,
driver.findElement(By.<your locator>));
完成此操作后,您可以尝试:
正常点击()功能:
driver.findElement(By.<your locator>).click();
OR
非原生javascript执行者:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.<your locator>));
答案 1 :(得分:1)
Prateek的回答是正确的。然而,我在最新版本的Firefox和Selenium 2.50.1中注意到的一点是,它并不总是能够成功地将元素滚动到视图中。
如果您的问题是元素从屏幕滚动(并且因此在标题栏之类的内容下滚动),您可以尝试将其滚动到视图中,如下所示:
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY();
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
如果需要,还可以添加静态偏移量(例如,如果页面标题高200像素且始终显示):
public static final int HEADER_OFFSET = 200;
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}