3
  1. Go to http://live.guru99.com/
  2. Click on my account link
  3. Click Create an Account link and fill New User information except Email ID.

Hi..Above is my test case.Unable to create xpath for account link.

Here is script i tried:

WebDriver Driver=new FirefoxDriver();                 
Driver.get("http://live.guru99.com/");
WebElement element=Driver.findElement(By.linkText("My Account"));       
element.click();

It would be great if someone helps me.

Thanks in advance.

1

4 Answers 4

12

Your code is almost correct.
BUT By.linkText works on the link text as you see it with your eyes on the website (after all CSS is applied).

If you have a look at http://live.guru99.com/ you will see that the link you are searching for is in capital letters:

"MY ACCOUNT"

But you tried to find "My Account".

So just change the third line in your code to:

WebElement element=Driver.findElement(By.linkText("MY ACCOUNT")); 

And everything should work just fine!

Be careful!

I see people answering using the following code:

Driver.findElement(By.xpath("//a[@title='My Account']"))

This is dangerous! Why? Because there are two elements that fit this findElement query!! Check it for yourself by running the following code:

System.out.println(Driver.findElements(By.xpath("//a[@title = 'My Account']")).size());

This returns 2! (First link is directly visible as "MY ACCOUNT", the other one can be seen if you click on "ACCOUNT" then in the dropdown there is another link visible as "My Account". Both links have the same title but are different elements).

We are lucky that in this case, both elements bring you to the same desired location.

BUT in my experience this is usually not the case. On many poorly written websites you have several elements with the same title, then it is pure luck, if the element that is returned by this kind of query returns the element that you wanted.

For this reason it is always a good idea to check how many Elements there are that fit your query using "findElements".

Even safer

Let's say one day this guru99 site would decide to use uppercase for both links. Then also the approach of this answer would suffer from this problem.

That's why it might be a good strategy to use another locator to narrow the search for the link down.

Analyzing the site I would go for "footer":

WebElement footer=Driver.findElement(By.className("footer")); 
WebElement link=footer.findElement(By.linkText("MY ACCOUNT")); 
Sign up to request clarification or add additional context in comments.

5 Comments

...which is interesting because if you look at the HTML, it's in mixed case. It's the CSS that causes it to be uppercase. An alternative is to look for title="My Account".
@JeffC if you read my answer, that's exactly what I am talking about! If you use "By.linkText" you should not! look at the HTML code but at what you see with your eyes on the site after all CSS is applied. Since OP was wondering that his "By.linkText" is not working, it's because he wasn't searching for the uppercase linkText
@JeffC why did I get lucky? would you be so kind to explain? There is only one element returned for my query, but 2 elements are returned for JordRoss's query. I don't understand what you mean.
@himaja glad to be of help!
@drkthng you are right... I was exploring the page with javascript and looking at the actual HTML and saw that there was a hidden link that has the same LinkText which would make 2 links. I wasn't thinking at the time that Selenium would only see the one. So as long as the state of the page doesn't change and show the other link you aren't lucky... ;)
1

I've certainly had times where locators that I think should work don't. This was one of those cases for me too. So I tried a few other things. This works for me:

    WebDriver Driver=new FirefoxDriver();
    Driver.get("http://live.guru99.com/");
    Driver.findElement(By.xpath("//a[@data-target-element='#header-account']")).click();
    Driver.findElement(By.xpath("//a[@title='My Account']")).click();

3 Comments

@himaja that's great, you're welcome. Since this solved your issue perhaps you would be so kind as to select it as the answer? (By clicking the checkmark.) Thanks!
@EGHM: himajas locator is working just fine! BUT one needs to use the linkText as you see it on the website (after all CSS is applied) not as it is in the HTML code. So his code line with uppercase Driver.findElement(By.linkText("MY ACCOUNT")); works just as expected
@EGHM be aware that your second findElement fits for two elements on the site!
0

WebElement element = Driver.findElement(By.xpath("//a[@title='My Account']"));

2 Comments

this is not correct! because there are two elements on the site that meet your condition, so getting the right one is pure luck -> try this and you will understand: System.out.println(Driver.findElements(By.xpath("//a[@title = 'My Account']")).size());
This does result in two elements. It depends which one you want to click :) if you want to click the second link use: //*[@class='footer-container']//a[@title='My Account'] Otherwise you will need to access the first My Account link in the Account menu: //*[@class='page-header-container']//a[@title='My Account']
-2

There is no 'My Account' link, rather it is 'Account'. So, change -

WebElement element=Driver.findElement(By.linkText("My Account"));

to -

WebElement element=Driver.findElement(By.linkText("Account"));       

4 Comments

WebElement element=Driver.findElement(By.linkText("Account"));
Try this - WebElement element=Driver.findElement(By.className("skip-account"));
When i use this xpath it is pointing my account which is under account.bt my testcase is to click on my account which is one the bottom of the page.
There is both an Account and a My Account link. The question was for the My Account link.

Your Answer

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.