This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

xpath for href for selenium web elements  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to find a href link for selenium web elements, made some tries but doesn't seem to be working.

href link as follows :
<div data-v-5fa222fc="" class="user-menu">
     <a data-v-5fa222fc="" href="logout.php">Logout</a></div>

I have tried the following way :

By.xpath("//a[contains(@href='logout.php')]")
By.xpath("//a[@href='logout.php']")
By.xpath("//a[contains(@href='logout.php')]")


 
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arya Ramanujan wrote:
By.xpath("//a[contains(@href='logout.php')]")


Try this:

By.xpath("//a[contains(@href='logout.php')]/@href")
 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Arya Ramanujan wrote:
By.xpath("//a[contains(@href='logout.php')]")


Try this:

By.xpath("//a[contains(@href='logout.php')]/@href")



I have tried it but still failing.
 
author & internet detective
Posts: 39208
726
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Contains takes two parameters. So it would be

 
Jeanne Boyarsky
author & internet detective
Posts: 39208
726
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that my previous post is just the syntax in xpath. I didn't think you could return an attribute as a webelement in Selenijm. So I tried it

This complains that I'm trying to return an attribute rather than a tag.


This means that you need to use


That will return you the anchor elements. Then you'll get the attribute for "href" from within Java or whatever language you are calling the Selenium code form.
 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeanne,
I have tried the following as suggested :


I am able to see the logout link but unable to click it. Also tried adding explicit wait, with no success.
The logout link is only seen when the profile link is clicked. I am able to click the profile link to see the logout but unable to click it.


Jeanne Boyarsky wrote:Note that my previous post is just the syntax in xpath. I didn't think you could return an attribute as a webelement in Selenijm. So I tried it

This complains that I'm trying to return an attribute rather than a tag.


This means that you need to use


That will return you the anchor elements. Then you'll get the attribute for "href" from within Java or whatever language you are calling the Selenium code form.

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to obtain the href attribute of the link with text Logout the relevant XPath query would be something like:



References:

  • XPath Syntax
  • Using the XPath Extractor in JMeter
  • XPath Examples

  •  
    Arya Ramanujan
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am sorry, I have tried a lot but it did not work. I have a complex and confusing framework in my system.
    Junilu suggested implementing POM model. So, I am in the process of overhauling it now.
    I will definitely try this with the new framework and post my response soon. My worry is if Logout itself is not identifiable, test may not even move further.
    I will also post my code so that it is easy to review, if it does not work.
    I appreciate you taking time for helping this out.

    Dmitri Tikhanski wrote:If you need to obtain the href attribute of the link with text Logout the relevant XPath query would be something like:



    References:

  • XPath Syntax
  • Using the XPath Extractor in JMeter
  • XPath Examples

  •  
    We noticed he had no friends. So we gave him this tiny ad:
    global solutions you can do at home or in your backyard
    https://www.kickstarter.com/projects/paulwheaton/better-world-boo
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!