Unable to locate element using xpath | Selenium Forum
M
Posted on 07/11/2015
Hi Ashish,

I am trying to locate an element using xpath in the website w3school.com

public class AdvancedXPath {
public static void main(String[] args) {

WebDriver driver=new FirefoxDriver();
driver.get("http://www.w3schools.com/"); driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id='sidemenu']/a[text()='Learn Bootstrap']")).click();

I am using the text property for web driver to recognize the element

When i run this snippet the webdriver goes to w3.schools.com ,maximizes the window and highlights the Learn Bootstrap code but it is not getting clicked as per your example.



I am using the latest version of firefox 42.0



Any workaround for this? Could not get why the Learn Bootstrap link is not getting clicked.

Getting the following exception in the console

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 12 milliseconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'skash', ip: '192.168.0.10', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=42.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b8d21dd8-9ecb-4c12-bfc4-8a6e6f415979
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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:647)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at TestScripts.AdvancedXPath.main(AdvancedXPath.java:16)
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'skash', ip: '192.168.0.10', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_51'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/Sharath/AppData/Local/Temp/anonymous1113688835234539177webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/Sharath/AppData/Local/Temp/anonymous1113688835234539177webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Sharath/AppData/Local/Temp/anonymous1113688835234539177webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/Sharath/AppData/Local/Temp/anonymous1113688835234539177webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/Sharath/AppData/Local/Temp/anonymous1113688835234539177webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)


Please suggest

M
Replied on 08/11/2015

Since WebDriver tries to simulate real users, it cannot interact with elements which are invisible/hidden.


instead try to use this xpath [color=#FF0000:twi514kc]//*[@id="sidemenu"]/a[5][/color:twi514kc]


M
Replied on 13/11/2015

You have to provide wait here, it worked smoothly -

WebDriver driver=new FirefoxDriver();
driver.get("http://www.w3schools.com/");
[b:2cz23emc]driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);[/b:2cz23emc]
driver.manage().window().maximize();
driver.findElement(By.xpath("[b:2cz23emc]//*[@id='sidemenu']/a[text()='Learn Bootstrap'][/b:2cz23emc]")).click();