How do i get the URL of the attribute ONCLICK using Xpath? - xpath

How can I extract the value of an attribute node via XPath?
Input HTML file is:
<tr class="evenrow" onclick="GoToLink('http://www.example.com/136.html');"></tr>
i wanna get this output : http://www.example.com/136.html
thank you for your help

Here is an answer using a combination of substring-before and substring-after:
substring-before(substring-after(//tr/#onclick, "'"),"'")
Basically what it does is it takes the text inside the onclick attribute then substrings the URL between the ' single quotes (for double quotes you'll need to adjust it).

Try following one :
driver.findElement(By.xpath("//a[#onclick='GoToLink()']")).getText();
Hope it will help you.

Related

How to extract text using xpath?

I am very new to Xpath and html. Could anyone please tell me how to use xpath to extract the text ('Nicht verfugbar') shown in the attached pic?
Thanks a lot!
The web page I am talking about here is
https://de.louisvuitton.com/deu-de/produkte/nano-speedy-monogram-010575
You can try with
//span[#class='lv-stock-indicator lv-product-stock-indicator lv-product__stock list-label-m -not-available']/text()
The class of the span is unique, so it can be used for location.
More info:
https://www.guru99.com/xpath-selenium.html

XPATH - how to get the text if an element contains a certain class

JHow do I grab this text here?
I am trying to grab the text here based on that the href contains "#faq-default".
I tried this first of all but it doesn't grab the text, only the actual href name, which is pointless:
//a/#href[contains(., '#faq-default-2')]
There will be many of these hrefs, such as default-2, default-3 so I need to do some kind of contains query, I'd guess?
You are selecting the #href node value instead of the a element value. So try this instead:
//a[contains(#href, '#faq-default-2')]

How to locate an element having href='#' attribute in anchor tag

abc
I am not able to locate above element. I tried //*[#id="contact-groups"], but with no success.
Well, XPath is not the best of the methods to find elements. But following will match the links with href = "#".
//a[#href="#"]
Not sure if your scenario is as simple as your question. I did test this with a simple html page.
The locator seems correct. You could try this too:
.//*[#id='contact-groups']
As per HTML code provided you can try below xpaths
//a[#id='contact-groups']
//a[contains(text(),'abc')]
//a[#href='#']
Thanks
I am guessing the link is in an Iframe.
Use the following code to switch to the frame and then click on the link
driver.switchTo().frame("frame-name");
driver.findElement(By.xpath("//a[#id='contact-groups']")).click();

Unable to locate the correct element using xpath

I am facing issue in selecting a particular drop down from the webpage.
I need to select the second highlighted div tag in the image above.
The xpath that I am trying to use is:
//div[#class='page-container']//table//div[#class='ui-multiselect-selected-container']
Kindly suggest how can I edit the xpath to select the second div tag.
I am new to xpaths and any help will be appreciated.
I think the below xpath should work to locate the second instance
(//div[#class='ui-multiselect-selected-container'])[2]
The second row has a tr class. When you use //div[#class='page-container']//table//div[#class='ui-multiselect-selected-container'] since your are referring to relative div(using //) it points to 1st element found by default.
I see that the tr element for the second multiselect has a class attribute, which makes unique
so, this will be //div[#class='page-container']//table//tr[#class='rowRelativeTo']//div[#class='ui-multiselect-selected-container']
You can also use the style elements which are unique:
//div[#class='page-container']//table//tr[#class='rowRelativeTo']//div[#style='float:right'] /div[#class='ui-multiselect-selected-container']

Referring to "title" of <a> in Xpath request

I'm making an Xpath as part of a scraping project I'm working on. However, the only defining feature of the text I want is the title attribute of the enclosing <a> tag like so:
This is what I want to scrape
Is it at all possible to refer to that title and create a path like this?
//tr/td[style='vertical-align:top']/a[title='Vacancy details']
Attributes in XPath expressions need to be prefixed with the # symbol...
//tr/td/a[#title='Vacancy details']
//tr/td/a[#title='Vacancy details']/#title
You can grab just the title if that's all you want

Resources