0
votes

I'm trying to click on a link (Which looks like a tab button) using Xpath but getting an error.

This is the html snippet :

<div id="tile_8" style="height: 93px; width: 26%; background-color: rgb(45, 87, 19); color: white; position: relative; float: left; margin: 10px; padding: 5px; cursor: pointer; z-index: 1; border: 2px solid white;" class="divAppList-shadow" onmouseover="mouseOvering(event,'8')" onmouseout="mouseOuting(event,'8')">
	<b>
	<span style="font-size:medium; z-index:1; color:white;">Suggestion Scheme</span>
	</b><br>
	<span style="text-align:justify; z-index:1;color:white;">There is always a better way of doing a thing. You only can suggest it for us to Improve.</span>
 </div> 

I have tried the following code :

driver.findElement(By.xpath("/html/body/div[5]/div[2]/div")).click();

This is the Error :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/div[2]/div"}
(Session info: chrome=54.0.2840.87) (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 412 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700' System info: host: 'tmlpnedtp061674', ip: '172.22.84.78', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', 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.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\AMITPA~1.TTL\AppData\Local\Temp\scoped_dir11224_7493}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=54.0.2840.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] Session ID: 2bb2aa6d378f2c0f6ec720a9ee87068c *** Element info: {Using=xpath, value=/html/body/div[5]/div[2]/div} 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:216) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) at testProj001.CordysUpgradeDemo.main(CordysUpgradeDemo.java:19)

Screenshot of the Site:: enter image description here

In the Screenshot as you can see i am trying to click on The Suggestion Scheme Button Link

4
Please help will be appreciated as i am new to selenium automation.Amit
Share HTML code for mentioned buttonAndersson
You are using positional xpath which would not be more stable, for better solution you need to share relevant HTML..Saurabh Gaur
Can i share you the HTML here if that would help??Amit
@AmitPandey you can share HTML after editing your question..Saurabh Gaur

4 Answers

1
votes

You're probably not waiting for the page to load completely. Try to wait for the page to completely load before trying to click on it. If the xpath is correct, then it has to be that.

To test, go to the webpage using Chrome..press F12 to open the developer's tools. Go to the console tab and type in the following: $x("/html/body/div[5]/div[2]/div") check the results. if it returns the correct element, then you have the correct xpath. If not, try the following:

  1. //div[@id='tile-8']

  2. //div[@id='tile-8']/span[text()='Suggestion Scheme']

  3. //span[text()='Suggestion Scheme']

If option 1 works, just use By.id("tile-8") instead of By.xpath()

1
votes

I can see you have been using an autogenerated Xpath from firepath. I guess so.

Most of the times those Xpath will have browser compatability issue. Start writing your dynamic Xpath and before using that xpath in script make sure the xpath is correct by checking it in Firebug or in Chrome developer option.

Steps to navigate to Developer option in chrome:

  1. Open chrome and navigate to the application.
  2. Press F12.
  3. Now you will see the developer console. Click ctrl+F and paste the Xpath you have written. If the Xpath is correct you will see the matching node results highlighted in yellow color.

Let me know if you have any questions.

0
votes

You didn't provide anything to help you with your problem, I would give you -point, because now it is guessing game.

What can I say:

  1. you did not provide full html of the page, only the button and it does not help. Since you are using Chrome DO NOT use firepath but the following:

    RMB (on button)->inspect->RMB on highlighted part->Copy->Copy Xpath

    And check if xpath is the same

  2. If it is the same it may be caused by static method you are using, driver is not that intelligent to press the button that is behind something or you have to scroll down to press it. That's why I wanted screenshot

If you need to scroll down page to press it add following before driver.findElement():

((JavascriptExecutor)driver).executeScript("scroll(0,250)");  //250 is value of page scrolled, depends how much

As said, it is my guess since you didn't provide any information

0
votes

You can generate Xpath installing "ChroPath" on to the browser.