简体   繁体   中英

Selenium Webdriver - element not clickable error in firefox

The element not clickable error that usually appears in chrome driver happens to be showing in firefox also. The error message shown:

  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)

You should probably wait for the element to be clickable, You can use:

WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedConditions.elementToBeClickable(By.<your locator>));

OR Sometimes you will even need to hover over the element to make it clickable. This you can do by this:

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>));

After doing this you can try :

Normal click() function:

driver.findElement(By.<your locator>).click();

OR

Non-native javascript executor:

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();",  driver.findElement(By.<your locator>));

Prateek's answer is correct. However something I've noticed with the latest version of Firefox and Selenium 2.50.1 is that it's not always scrolling the element into view successfully.

If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:

private void scrollToElementAndClick(WebElement element) {
    int yScrollPosition = element.getLocation().getY();
    js.executeScript("window.scroll(0, " + yScrollPosition + ");");
    element.click();
}

if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):

    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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM