xpath-selecting link by text - html

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.

Related

How can I get the element of a-tag in the div class with selenium?

I recently work on the project that I have to get the element from a specific website.
I want to get the text elements that are something below.
<div class="block-content">
<div class="block-heading">
<a href="https://www~~~~~~">
<i class="fa fa-map">
::before
</i>
"Text I want to get"
</a>
</div>
</div>
I have been trying to solve this for a while, but I could not find anything working fine.
I would love you if you could help me.
Thank you.
According to the information you provided the text you are looking for is inside a element so the xpath for this element is something like:
//a[contains(#href,'https://www')]
But since there is also i element inside it, getting the text from a element will give you both text contained in a itself and the text inside the i.
So you should get the text from i that is looking like just a (space) here and reduce it from the text you are receiving from the a.
In case you want to perform this action on all the a elements containing href and i element inside it you can use the following xpath:
//a[#href and ./i]
If there are more specific definitions about the elements you are looking for - the xpath I mentioned should be updated accordingly
From your comment, I understood that you would like to extract that text. So here is the code for you which would extract the text you want.
Selenium::WebDriver::Wait
.new(timeout: 60)
.until { !driver.find_element(xpath: "//i[#class='fa fa-map-marker']/..").text.empty? }
p driver.find_element(xpath: "//i[#class='fa fa-map-marker']/..").text[/(?<=before \")\w+ \w+ \w+ \w+ \w+/]
output
"Text I want to get"
I couldn't get the elements that I wanted directly, so here's what I did.
It is just that I did modify the elements with some methods though.
def seller_name
shop_info_elements = #driver.find_elements(:class_name, "block-content")
shop_info_text= shop_info_elements.first.text
shop_info_text_array = shop_info_text.lines
seller_name = shop_info_text_array.first.chomp
seller_name
end
It is not beautiful, but it can work for any other pages on the same site.

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

Simple Xpath puzzle

I'm trying to automate the Google Translate web interface with Selenium (but it's not necessary to understand Selenium to understand this question, just know that it finds elements and clicks them). I'm stuck on selecting the language to translate from.
I can't get to the point where the drop-down menu opens, as seen in the screenshot below.
Now, I want to select 'Japanese'.
This xpath expression works: $b.find_element(:xpath,"//*[#id=':13']/div").click But I would rather have one where I can just input the name of the language.
This xpath expression also works: $b.find_element(:xpath,"//*[contains(text(),'Japanese')]").click But only as long as there is no other 'Japanese' text on the page.
So I'm trying to narrow down the scope of my xpath, but when I try to specify the path to take to find the 'Japanese' text, the expression no longer works, I can't find the element: $b.find_element(:xpath,"//*div[#id='gt-sl-gms']/*[contains(text(),'Japanese')]").click
It also no longer works for the original xpath either: $b.find_element(:xpath,"//*div[#id='gt-sl-gms']/*[#id=':13']/div").click
Which is weird, because to bring down the drop-down menu, I use this xpath $b.find_element(:xpath,"//*[#id='gt-sl-gms']/*[contains(text(),'From:')]").click.
So it's not that I have two wildcards in my expression and it's not that my expression is too specific. There's something else that I'm missing and I'm sure it's really simple.
Any suggestions are appreciated.
Edit Other things I have tried unsuccessfully:
$b.find_element(:xpath,"//*/div[#id='gt-sl-gms']/*[#id=':13']/div").click
$b.find_element(:xpath,"//*[#id='gt-sl-gms']/*[#id=':13']/div").click
$b.find_element(:xpath,"//*[#id='gt-sl-gms']//*[#id=':13']/div").click
If the div with "#id=':13'" is an descendant of the div with "#id='gt-sl-gms" your xpaht "//*[#id='gt-sl-gms']//*[#id=':13']/div" would work.
The above xpaht expect that the html looks somehow like:
<div id="gt-sl-gms">
<div>
<div id=":13">
<div></div>
</div>
</div>
</div>
If <div id="gt-sl-gms"> in not an ancestor (as I expect) you have to look for an "real" ancestor, or you may use following (for nodes later in the document) or following-sibling (for nodes later in the document at the same level as the previous.
*div is incorrect, it should be just div. Also, depending on he structure of the HTML, you may need // instead of /.
Try selecting descendants (//) instead of (/*) which is really grandchildren or deeper.

Xpath and innerHTML

What Xpath expression can I use to find all the anchor (just 'a') elements whose actual text (the innerHTML) is Logout.
something like
//a[#innerHTML='Logout']
Would that be correct?
No, it would be incorrect. innerHTML is a property, part of the object model, while XPath operates on tags and attributes. Unless your a tag actually has an attribute named innerHTML, this wouldn't work.
If you want to compare the value of the tag itself, you can use the . (dot) to refer to the tag:
a[.='Logout']
However, I must add, just in case you're using jQuery: I'm not sure if it will work with jQuery. jQuery does not support XPath fully, only basic stuff.

Selenium: Not able to understand xPath

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.