Im using HtmlAgilityPack/HAP so that I can use Xpath with HTML documents.
I need help selecting the preceding-sibling of div class="address" in this url:
www.yellowpages.ca/search/si-geo/1/sh/Ottawa,+ON
The sibling that I want is h3 class="listingTitleLine"
Here is a screenshot:
http://i55.tinypic.com/25gc4qo.png
Can I get some help please.
-Dd,
To select the preceding-sibling H3 element with the class attribute value of "listingTitleLine":
preceding-sibling::h3[#class='listingTitleLine']
To select each of the H3 elements with a class attribute value of "linstingTitleLine" that are preceding-siblings of div elements with a class attribute value of "address":
//div[#class='address']/preceding-sibling::h3[#class='listingTitleLine']
Related
Please find the HTML :
<div id="086" data-activity-type="CompatCheck" class="Activity"></div>
Here only constant value is data-activity-type="CompatCheck" and classname is same for all the other elements.
while trying to print id using data-activity-type i.e. CompatCheck using xpath
Expected output:
086
Problem 2 : Observed that the only unique value is id, how can we fetch in this case.
Please Find the HTML :
<div id="0007" data-activity-type="CompatCheck" class="Activity"></div>
<div id="110007" data-activity-type="CompatCheck" class="Activity"</div>
While trying to use following code line :
findElement(By.xpath("//div[#data-activity-type='CompatCheck']")).getAttribute("id");
I'm getting only first id i.e; 0007
but I need always the second id="110007", can you please suggest to get the second id
As the attribute data-activity-type="CompatCheck" is unique you can use it and you can use either of the following the following locator strategies::
Using cssSelector:
System.out.println(driver.findElement(By.cssSelector("div[data-activity-type='CompatCheck']")).getAttribute("id"));
Using xpath:
System.out.println(driver.findElement(By.xpath("//div[#data-activity-type='CompatCheck']")).getAttribute("id"));
Console output:
086
With XPath or CSS Selectors you can locate web element based on any constant attribute value. I.e. it can be based on constant class name value or id or any other attribute. In your case the constant attribute value is data-activity-type="CompatCheck". So, you can locate this element by XPath or by CSS Selector and then extract the id attribute as following:
XPath
driver.findElement(By.xpath("//div[#data-activity-type='CompatCheck']")).getAttribute("id");
CSS Selector
driver.findElement(By.cssSelector("div[data-activity-type='CompatCheck']")).getAttribute("id");
I want to access the 2nd element with same class name using css selectors.
1st element:
<a class="good">
2nd element:
<a class="good">
Css selector I am using :
a.good
but this accessing both of them.
How to access the 2nd one or anyone individually?
you can use pseudo selector, pseudo selector matches elements based on their position among a group of siblings.
check example link below
click here
I am trying to get the xpath of a svg that has an attribute <use href= "#icon-map">
So far the path //*[local-name()='svg']/*[local-name()='use'] works, but it finds 84 entries.
How can I modify the xpath in order to select only the use that has the href as "#icon-map"?
You can use this:
//*[local-name()='svg'][use[#href="#icon-map"]]
or
//*[local-name()='svg'][*[local-name()='use'][#href="#icon-map"]]
See example.
If you have more results than you expect then you should use more specific paths to the element or take your query into (..) and add number of an item into [..] like :
(//*[local-name()='svg'][use[#href="#icon-map"]])[2]
If use is an attribute then you could do this :
//*[name()='svg']//*[#use and #href='#icon-map']
Also the above solution assumes that #icon-map is unique in HTML DOM
I need to detect via xpath an element (image with href) but i know a part of href only
This is the html
<img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60">
I know the id 123456 (part of href)
I tried this xpath that recognize element with a part of href but working in text link only
xpath=//a[contains(#href, "123456")]
How can i detect the element using a part of href only ?
I need xpath only please.
You need get /img in //a where href attribute contains() or ends-with() your id
This is XPATH that you need. At least i would use this XPATH in this situation
//a[ends-with(#href, 'your-id-here')]/img
You can use regular expresions. Something like
driver.find_element_by_xpath("//input[starts-with (#name,'Tut')]")
or as you described
driver.find_element_by_xpath("//input[contains(#name,'sel')]").
Be awere of one thing, do not use double quotes, for the string you are searching as atribute value. Use a single quote like I previously described.
I am wondering if below is achievable using xpath
Given:
<label for="pt1:sc">Select Country</label>
<select id="pt1:sc">....</select>
Requirement:
I want to find select element using single xpath expression like below,
bcs ids are dynamic and always available in attribute 'for'.
//label[text()='Select Country']/#for//*[#id=#for]
Can we pass attribute value(here for attribute of label) in xpath, further down the path to find element.
Please do not suggest alternative using siblings, child, id or selenium get-attribute etc.
Thanks,
You can use something like this to select an element with an attribute value which refers to another attribute located in another element :
//*[#id=//label[text()='Select Country']/#for]
I'm not sure how it's going to work with your actual html, but it works on the example in the question:
//label[text()='Select Country'][#for=//select/#id]