• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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: 13458
222
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: 39283
727
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: 39283
727
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

  •  
    I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!