2

I've a question, how can I capture the xpath in the case below?

I tried this:

driver.findElement(By.xpath("//*[@id='L2_auto_8']/div/a[1]")); 

and I tried:

driver.findElement(By.xpath(//a[@href='/principal.aspx']));

but I received this error: org.openqa.selenium.NoSuchElementException

Screenshots 1
Screenshots 2
Screenshots 3

3
  • Do your xpaths work in browser dev-tools? And whether your test reachs the page you're facing an issue at?
    – Alexey R.
    Mar 15, 2018 at 11:25
  • yes, look at my screenschot [3] , when i search xpath in dev-tools
    – skape
    Mar 15, 2018 at 14:30
  • Did you try waiting until the page is loaded properly? Try a sleep method. Then you can find out whether the page is loaded or not properly. If it works, try to use expected condition to wait until the element is available. Else the problem is with xpath.
    – Syrus
    Sep 9, 2019 at 10:36

5 Answers 5

1

Change this line

driver.findElement(By.xpath(//a[@href='/principal.aspx']));

to

driver.findElement(By.xpath("//a[@href='http://www4.tiri.ius.br/hconsportal/principal.aspx']"));

or

driver.findElement(By.xpath("//a[contains(text(),'Dados Pessoais')]"));

It would work.

0

I'm not sure why your first xpath fails, it looks okay to me, but your second one fails because you're looking for an exact match, but only showing part of the URL and in your example, at least, is missing quotes.

WebElement lnk = driver.findElement(By.xpath("//a[ends-with(@href,'/principal.aspx')]"));
0

Try this Xpath. Xpath by using the text in the a tag

"(//a[contains(text(),'Dados Pessoais')])[give the occurrence of this text]"

for example if this xpath matches with multiple elements then give the number where it matches the exact. If it matches with the 4th element then give as below

"(//a[contains(text(),'Dados Pessoais')])[4]".
0

Try this xpath:

//a[contains(text(),'Dados Pessoais')]

If your script originated from a page redirect, try adding explicit wait

0

We as a company are providing automation testing services. Our approach is to use Xpath as last priority i.e when no other option is available like ClassName, ID, LinkText, CSS etc. Here we can directly use "LinkText" and above can be make functional like using the below syntax.

WebElement link = driver.findElement(By.linkText("Dados Pessoais"));
link.click();

Also like if you want to use the Xpath only then very first xpath example you share is correct. Don't know why it is not working, you can try by adding some wait for elements to load properly. Also you can use below mentioned Xpaths(using full value).

WebElement link = driver.findElement(By.xpath("//div//a[@href='http://wwwh4.tiri........./principal.aspx']"));
link.click();

WebElement link = driver.findElement(By.xpath("//div//a[Contains(@href,'/principal.aspx')]"));
link.click();

WebElement link = driver.findElement(By.xpath("//*[contains(@href,'/principal.aspx')]"));
link.click();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.