3
votes

I Have a problem on Selenium in click event

WebElement btn = driver.findElement(By.xpath("//form[@name=\"addMemberForm\"]/div[14]/div/button"));
System.out.print(btn);

the result of the print

[[ChromeDriver: chrome on LINUX (0f787065444f6b2cddf2c6ff5e6c056d)] -> xpath: //form[@name="addMemberForm"]/div[14]/div/button]

and when triggered with btn.click() event error says element not visible

org.openqa.selenium.ElementNotVisibleException: element not visible (Session info: chrome=52.0.2743.82) (Driver info: chromedriver=2.23.409687 (c46e862757edc04c06b1bd88724d15a5807b84d1),platform=Linux 4.2.0-42-generic x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 11 milliseconds Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700' System info: host: 'kyben-All-Series', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.2.0-42-generic', java.version: '1.8.0_101' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.23.409687 (c46e862757edc04c06b1bd88724d15a5807b84d1), userDataDir=/tmp/.org.chromium.Chromium.8Xt3v7}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=52.0.2743.82, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 0f787065444f6b2cddf2c6ff5e6c056d at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 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:683) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:319) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85) at testCase.teamMemberNew2.chngPassLogout(teamMemberNew2.java:127) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) at org.testng.TestRunner.privateRun(TestRunner.java:774) at org.testng.TestRunner.run(TestRunner.java:624) at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at org.testng.SuiteRunner.run(SuiteRunner.java:261) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at org.testng.TestNG.run(TestNG.java:1048) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)

3
Make sure provided xpath locate only one button which you want...Saurabh Gaur
Open your browser console by pressing f12 in that page and paste this $x("//form[@name='addMemberForm']/div[14]/div/button") and let me know the result..Saurabh Gaur

3 Answers

2
votes

Few things you can check:

  1. Obvious ones: Is the button visible when you click it? Is the element locator correct?

  2. Is there any other element on the page with the same xpath? It may be possible that an element with same xpath is located on some background page/ or is part of some hidden widget. As a result, findElement() is actually pointing to the hidden button.

  3. Have you tried giving an unique id to the button itself? That will help you to locate the button uniquely.

2
votes

You can use explicit wait to wait for the button to be visible before the clicking

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement btn = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//form[@name=\"addMemberForm\"]/div[14]/div/button")));
btn.click();

This will wait up to 10 seconds for the button to be visible.

You can also try moving to the button before the clicking

WebDriverWait wait = new WebDriverWait(driver, 10);
Actions action = new Actions(driver);

WebElement btn = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//form[@name=\"addMemberForm\"]/div[14]/div/button")));
action.moveToElement(btn).perform();
btn.click();
1
votes

Sometimes the error message "element not visible" means you object may not be placed in your current browser window view. You may need to scroll the browser to in order to locate your object before you click. If that is the case, you may need to scroll your browser to center your object before you click. You can add the following code before button click.

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(false);", btn);

Note that "btn" is your WebElement object. The above code works in my Firefox browser. But I saw some other answers suggests to set scrollIntoView(true);. You may try both and see which one works better.