Selenium: Not able to understand xPath - html

I have some HTML like this:
<h4 class="box_header clearfix">
<span>
<a rel="dialog" href="http://www.google.com/?q=word">Search</a>
</span>
<small>
<span>
<a rel="dialog" href="http://www.google.com/?q=word">Search</a>
</span>
</h4>
I am trying to get the href here in Java using Selenium. I have tried the following:
selenium.getText("xpath=/descendant::h4[#class='box_header clearfix']/");
selenium.getAttribute("xpath=/descendant::h4[#class='box_header clearfix']/");
But none of these work. It keeps complaining that my xpath is invalid. Can someone tell me what mistake I am doing?

You should use getAttribute to get the href of the link. Your XPath needs a reference to the final node, plus the required attribute. The following should work:
selenium.getAttribute("xpath=/descendant::h4[#class='box_header clearfix']/a#href");
You could also modify your XPath so that it's a bit more flexible to change, or even use CSS to locate the element:
//modified xpath
selenium.getAttribute("//h4[contains(#class,'box_header')]/a#href");
//css locator
selenium.getAttribute("css=.box_header a#href");

I had similar problems with Selenium and xpath in the past and couldn't really resolve it (other than changing the expression). Just to be sure I suggest trying your xpath expressions with the XPath Checker addon for firefox.

Related

How to select <div class="ok">.....<a href="soft://an.id/">...</div> nodes?

A document has several <div class="ok"> tags. I am able to select all of them with
"//*[#class="ok"]" (i don't have to specify div, because only div tags have this class). I get a list of 6 nodes matching this.
Now, i need
either to test each node in order to see if it includes the tag <a href="soft://an.id/">. This inclusion is not direct. I mean, the <div> includes a <table> with many <tr> and <td> and <span>, and the <a..> (only one, or none) somewhere before </div>.
or to directly select only (div) nodes of class="ok" that include this <a> tag.
I have tried many things, that all fail. Including protecting the "/" in the href detection (is it required?).
I am quite familiar with regular expressions, but i must confess that i find XPath syntax even harder to understand.. And the W3C reference documents are so hard, without examples..
Any hints are welcome.
In order to select only <div class="ok"> element containing <a href="soft://an.id/"> child element you can use the following XPath locator:
"//div[#class='ok' and .//a[#href='soft://an.id/']]"
If I understand you correctly, you have a nested somewhere under the div with class "ok", right?
So in xpath, the a / is meant for a direct locator under/above the current tag. If you are looking for the somewhere under the found div, you need to use:
//div[#class="ok"]//a[#href="soft://an.id/"]
Then you need to check if it exists or not by using some kind of an assertion.

How to search title in HTML using XPath?

I am trying to get 'RGF Administrator' text from the html example below. The difficulty is that title can take two values: 'Application Settings' or 'Параметры приложения'. At the same time, html may contain other elements with title = 'Application options'.
Which can i use xPath for it?
<div>
<button
title="Application options">
<span>
<span>
<bdi>
RGF Administrator
</bdi>
</span>
</span>
</button>
</div>
I use selenium, but I can't determine the xPath for 'RGF Administrator'.
xpath = "//*[#title='Параметры приложения' or #title='Application options']"
won't work since at the same time, html may contain other elements with title = 'Application options'.
Try this xpath expression on your actual xml and see if it works:
//div/button[#title = ('Параметры приложения', 'Application options')]//bdi/text()
If you are using selenium, then you need to use the get_attribute(string) method. So for example, when you find the elements xpath:
element = driver.find_element_by_xpath("xpath")
and then do
element.get_attribute("title")
to get Application options
How many bdi containers do you have on the webpage? If there is only 1, you can literally just use
//bdi
And you don't need any pathing.

Get Xpath for href from a tag based on span tag value

I am a beginner to xpath and I am unable to get XPath to get link from 'a' tag for below HTML code.
Get HREF value where span class value is "Upholstered" as shown in the snapshot.
Here, I want this value "/furniture/Bedrooms/Queen-Beds/_/N-8ddZ1z141u9?qf=styles_Upholstered" using Xpath.
Can you help me out please
According to your description the relevant XPath query to Get href value where span class value is "Upholstered will be something like:
//a[#class='Upholstered']/#href
However you forgot to add your actual HTML code (at least partial) so the above answer might not be 100% accurate.
Reference material:
XPath Language Specification
XPath Tutorial
Using the XPath Extractor in JMeter
Use below xpath to extract the URL of your <a> tag
//ul[#class='facetOptions']/li/a[#role='checkbox']/#href

xpath-selecting link by text

The HTML has a link as below:
<a class="alk" href="https://www.xyz.com/view?id=20221">my_color ⁄ color</a>
I am able to select it as:
//a[#class='alk']
and I want to be more definitive and select it as (since there could be more links as this):
//a[#class='alk'][text()='my_color ⁄ color']
The second selector is not returning me anything. Interestingly, the selector //a[#class='alk']/text() does return my_color ⁄ color. I am quite perplexed at what I may be missing in the selector above when trying to select by exact text().
If you want to select the whole link, you can adjust your XPath to
//a[#class='alk'][text()='my_color ⁄ color']
Result:
<a class="alk" href="https://www.xyz.com/view?id=20221">my_color ⁄ color</a>
The text is not an attribute, so the # is not needed.
Found the solution. FirePath is not able to resolve the xpath, but it does work perfectly fine with the application. I believe something is wrong with the FirePath xpath parser that is sometimes unable to resolve a "valid" xpath expression as this.

Finding XPath for text in div following input

I got an issue reading XPath. Need some help/advise from experts.
Part of my HTML is below:
<div class = "input required_field">
<div class="rounded_corner_error">
<input id="FnameInput" class="ideField" type="text" value="" name="first_name>
<div class ="help-tooltip">LOGIN BACK TO MAIN</div>
<div class="error-tooltip">
I need to find the XPath of the text message (LOGIN BACK TO MAIN)
Using Firebug I find the XPath
("//html/body/div/div[5]/div/div/form/fieldset/div/div[2]/div[2]/div/div");
But using above XPath I can read only class = help-tooltip but I need to read LOGIN BACK TO MAIN.
Try adding /text() on the end of the xpath you have.
It does not really look like your XPath matches your XHTML element.
You should try something simpler and more generic, such as:
//div[#class="help-tooltip"]/text()
See Selecting a css class with xpath.
I would use:
# Selecting the div element
//input[#id="FnameInput"]/following-sibling::div[#class="help-tooltip"]
# Selecting the text content of the div
//input[#id="FnameInput"]/following-sibling::div[#class="help-tooltip"]/text()
…since a syntactically-valid HTML document will have a unique id attribute, and as such that's a pretty strong anchor point.
Note that the latter expression will select the text node, not the text string content of that node; you need to extract the value of the text node if you want the string. How you do that depends on what tools you are using:
In JavaScript/DOM that would be the .nodeValue property of the text node.
For Nokogiri that would be the .content method.
…but I have no idea what technology you are using your XPath with.