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

asked Mar 14, 2018 at 17:44
3
  • Do your xpaths work in browser dev-tools? And whether your test reachs the page you're facing an issue at? Commented Mar 15, 2018 at 11:25
  • yes, look at my screenschot [3] , when i search xpath in dev-tools Commented 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. Commented 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.

answered May 28, 2022 at 3:48
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')]"));
answered Mar 14, 2018 at 20:16
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]".
answered Mar 16, 2018 at 6:38
0

Try this xpath:

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

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

answered Aug 9, 2019 at 11:43
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();
answered Sep 3, 2020 at 12:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.