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

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

Related

XPath for element by attribute value in Selenium?

For the following HTML:
Why does the following XPath doesn't work:
//option[value='0']
Is it related to value or to option element?
//option[value='0']
is not a valid selector incase you are attempting to identify/select the respective <option> element using Selenium.
Solution
You can use either of the Locator Strategies:
xpath:
//option[#value='0']
css_selector:
option[value='0']
tl; dr
Why should I ever use CSS selectors as opposed to XPath for automated testing?
Change
//option[value='0']
to
//option[#value='0']
because value is an attribute, not an element.
Your current xpath is searching for an option element with a child element value which in turn has contents 0. What you actually want to find is an option element with an attribute value with value 0:
//option[#value='0']

Get inner href of an href link with xpath

I just started out with Python and learning about xpath expressions.
I'm trying to get a div, an a class, look for the href inside the a class and then get the part of the href, then just continue with something.
div class: dropdown-menu and a class: dropdown-item
My url: https://www.something.com/library/category/stuff
My xpath expression: response.xpath("//div[#class='dropdown-menu']//a[#class='dropdown-item']//a[contains(#href, 'category')]")
It just returns an empty string and I can't figure out why, please advice.
Since an <a> can't really be nested inside an <a>, I suppose you meant to write two conditions for the same <a> here:
response.xpath("//div[#class='dropdown-menu']//a[#class='dropdown-item']//a[contains(#href, 'category')]")
That would be written like this:
response.xpath("//div[#class='dropdown-menu']//a[#class='dropdown-item' and contains(#href, 'category')]")
or like this (predicates, i.e. the filter conditions in the square brackets, can be chained and are evaluated one after another):
response.xpath("//div[#class='dropdown-menu']//a[#class='dropdown-item'][contains(#href, 'category')]")

css locator to navigate to parent tag from child and css locator to identify sub string in a attribute

I'm good at writing XPATH locators but for my new project i have to write locators using CSS
i have 2 doubts
1)
from the above code i have to identify div with id contains value "col3"
in xpath i can write //div[contains(#id,'col3')]
in css i tried below locators
css=#id:contains('col3')
css=div:contains(#id,'col3')
but none of them working.
please help me with CSS locator to identify id contains value "col3"
2)
i have to navigate from one attribute to its parent attribute.
i.e., from "Form tag i have to navigate ti its parent div tag"
in XPATH //form[#id='comment-form']/../
help me to navigate using CSS
Can you try this for 1st one
css=div[id*='col3']
The solution to your first problem is:
div[id*='col3']
Referring to Is there a CSS parent selector?, I do not think there is any solution for your 2nd problem. You might have to use xpath here.

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.

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.