2

Logout button

We are currently working on a selenium project. Can you guys help me to find the xpath locator for the logout button in the application?

Below is a screenshot of the code for the element for which I'm trying to find the xpath.

5
  • 'our application'? Add an some Id's.
    – Nathan
    Jan 29, 2016 at 7:40
  • 1
    Why was this voted to be an off topic question? Jan 29, 2016 at 8:12
  • Try this //div[@class="text" and text()="Logout"]
    – Nitesh
    Jan 29, 2016 at 9:49
  • "Below is a screenshot of the code" - The code is not visible. Was it removed in the process of editing the question?
    – ilm
    Feb 13, 2016 at 4:50
  • 4
    Possible duplicate of How to generate good locators for elements in Web pages?
    – dzieciou
    Feb 17, 2016 at 20:18

7 Answers 7

2

As @Nathan_Cooper already commented I would ask to add Ids to the objects.

If it's not possible, or if the HTML is dynamic, I would start a little higher than the other answer, in particular from

<div class="header_menu logout">

as I assume that a div with a class logout will always wrap the logout button.

//div[contains(@class ,'logout')]//div[contains(text(),'Logout')]
9

If you want to do it manually, then you will have to trace the path from the root of the host up to where the element is being generated.

OR

There is an easy way to get the xpath. In Mozilla FireFox install Firebug addon. Then install Firepath addon. Now visit your web page. Press F12 to open Firebug console. Click on the Firepath tab. Inspect the element you want to get the xpath of. Xpath of that element will be displayed in the Firepath console.

2

You can try below xpath

//*[@class='item']/div[text(),'Logout']    
0
1

The above answer by QAMember should be correct. You can also try this xpath

 //div[contains(text(),'Logout')]
0

Since the text Logout is what exactly we are looking for then no need to write text contains. You use below xpath.

 (By.xpath("//div[.='Logout']"));
0
//*[@class='item']//*[.='Logout']
  • keep it simpler, if there is are no text 'Logout' in any other class of type item, then above xpath would work.
  • No div or any other element provided
0

According to your question you are looking for button logout, not for the text.

So as we see in the image we will find the Button is a Link control inside the Div <div class="header_menu logout">.

the best match to locate the button is using xpath bellow.

//*[@class='header_menu logout']//a

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.