Unable to click a link on a website using Selenium and Java

498 Views Asked by At

I wish to click on the "Requests" link in the left panel of the website using selenium Java.

See the snapshot below where the xpathfinder shows element as:

/html/body/div[1]/nav/div/section/ul[3]/li/a/span[1]

enter image description here

Below is the part of my java code generated by selenium chrome browser IDE.

System.out.println("Title of the page is 7 -> Login");
driver.findElement(By.xpath("//li[@id='tv_3']/a/i")).click();
driver.findElement(By.linkText("Security Test Requests")).click();
driver.findElement(By.id("w0")).click();

Below is the view source of the page with the relevant part of the code having the Requests link.

</ul>
<ul class="sidebar-menu">
<li class="treeview" id='tv_3'>
<a href="#">
<i class="fa fa-edit"></i> <span>Requests</span>
<span class="pull-right-container" onclick="menu_dropdown('tv_3');"><i class="fa fa-angle-left pull-right"></i></span>
</a>
<ul class="treeview-menu">
<li><a href="/synvm/basic/web/index.php?r=request%2Findex-test-request"> Security Test Requests</a></li>
<li><a href="/synvm/basic/web/index.php?r=request%2Findex"> Exception Requests</a></li>
<li><a href="/synvm/basic/web/index.php?r=request%2Findex-change-request"> Change Requests</a></li>
<li><a href="/synvm/basic/web/index.php?r=request%2Findex-decommission-request"> Decommission Requests</a></li>
</ul>
</li>
</ul>

Edit: Uploaded the entire View Source of the webpage here: https://drive.google.com/file/d/1ov4Es3oDC66coaJADw6N1kxTorLmd3M6/view?usp=sharing

The issue is not with the page loading and wait of page. Infact the page gets loaded but the click on "Requests" shows a drop down which is not happening(The click)

I get the below error running my JAVA:

Title of the page is 7 -> Login
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Security Test Requests"}
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'VMINITSERMAPRAP', ip: '10.9.40.115', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Users\axmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:64908}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 3ca2879b253b55a953c0a481ec70ea78
*** Element info: {Using=link text, value=Security Test Requests}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

With the suggestion on this post I tried the below:

new WebDriverWait(driver, 40).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='sidebar-menu']/li[@class='treeview']/a//span[text()='Requests']"))).click();

But, I get the following error:

Title of the page is 7 -> Login
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //ul[@class='sidebar-memu']/li[@class='treeview']/a//span[text()='Requests'] (tried for 40 second(s) with 500 milliseconds interval)
        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
        at pack.SynvmRequest.testSynvmRequest(SynvmRequest.java:135)
        at pack.SynvmRequest.main(SynvmRequest.java:398)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//ul[@class='sidebar-memu']/li[@class='treeview']/a//span[text()='Requests']"}
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'MyHost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:53969}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 979b2c191659cbbba7c43ebe4db9b5b9
*** Element info: {Using=xpath, value=//ul[@class='sidebar-memu']/li[@class='treeview']/a//span[text()='Requests']}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

Can you please suggest?

2

There are 2 best solutions below

1
Norayr Sargsyan On

There is a space, and that's why selenium can not detect your element by linktext("Security Test Requests")

<li><a href="/synvm/basic/web/index.php?r=request%2Findex-test-request"> Security Test Requests</a></li>

your element should be

driver.findElement(By.linkText(" Security Test Requests")).click();

or you can use contains the method for XPath

 driver.findElement(By.xpath("//li[contains(text(), 'Security Test Requests')]").click();
3
undetected Selenium On

You need to consider a couple of things as follows:

  • The <a> element with text as Requests have a onclick event, so it's a JavaScript enabled element.
  • The <a> nodes with texts Security Test Requests, Exception Requests, Change Requests and Decommission Requests all of them have a leading white-space character. So linkText won't work.

Solution

To click on the element with text as Requests and then on the element with text as Security Test Requests you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • partialLinkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='tv_3']/a/i"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Security Test Requests"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='sidebar-menu']/li[@class='treeview']/a//span[text()='Requests']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='treeview-menu']//li/a[contains(., 'Security Test Requests')]"))).click();