Use HtmlAgilityPack to select elements without the "id" attribute - html-agility-pack

I would like to select all div elements that doesn't have an id attribute. I tried different combinations and Googled quite a bit, but I just can't get it right.
doc.DocumentNode.SelectNodes("//div[!#id]") <-- doesn't work

You can use XPath not() to achieve that, for example :
doc.DocumentNode.SelecteNodes("//div[not(#id)]")

Related

What is the proper way to use descendant in XPath

I am trying to find all DIV elements have the attribute widget-name and a descendant span tag that have a title attribute.
This is what I am trying.
//div[#widget-name and descendant::span[#title]]"
This seems to almost work but it is missing one element in the Nodes Collection it returns.
Never mind.
This is what I needed:
//div[#widget-name and descendant::span[#class='title']]
OK - take it back.
This is not the complete answer.
I am now trying to tweak this to where it returns all except where title is not equal to some text:
//div[#widget-name and descendant::span[#class='title' and [text()[contains(., '{someTextToKeep}'
Anyone see why this would be invalid XPath?
Final answer is:
//div[#widget-name and descendant::span[#class='title' and text()[not(contains(., 'someTextToKeep'))]]]"
This XPath should return all div's that:
has a widget-name attribute
has a descendant span element (used abbreviated syntax) that:
has a class attribute with the value 'title'
contains the text 'someTextToKeep' (if you want to exclude spans with certain text, wrap the contains() in not().
XPath:
//div[#widget-name and .//span[#class='title'][contains(.,'someTextToKeep')]]

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']

Retrieve value from within a div tag in xpath

I am trying to retrieve the value in the data-appid field, I have tried using following-sibling but its not really a sibling per se. Not sure how to go about retrieving this. Any pointers will really be great.
<div class="section app" data-appid="532054761" data-updateid="10184169">
The sibling axis applies to elements, not attributes.
You can reference data-appid simply as an attribute of the div element. For example,
//div/#data-appid
will select 532054761
If you need to be more specific about the particular div element for which you want its data-appid, you can use a predicate to select a particular div element. For example:
//div[#data-updateid='10184169']/#data-appid
You need to use getAttribute() method
Try the following code
String dataAppId = driver.findElement(By.xpath("//div[#class='section app']")).getAttribute("data-appid");
System.out.println(dataAppId);

How get AJAX elements which XPATH is varying dynamically

In my application one AJAX filed which is displaying some values
When I tried to find out their XPATH it is varying dynamically
for example :
First time when I tried to find out the path it is giving it as .//*[#id='ix-rt-13']. When I refresh the page it is giving it as .//*[#id='ix-rt-6'].
Actually it is displaying 2 values one with id .//*[#id='ix-rt-13'] and second one with .//*[#id='ix-rt-14']. And when I refresh the page it is giving XPath values as .//*[#id='ix-rt-6'] and .//*[#id='ix-rt-7'].
I want to retrieve the second element text. How to do that ?
<li class="ui-menu-item" role="presentation">
<a id="'ix-rt-15" class="ui-corner-all ui-state-focus" tabindex="-1">Being Powerful</a>
</li>
If I understand the question correctly, you can try this XPath :
(//*[starts-with(#id,'ix-rt-')])[2]
Above XPath will search for all elements with id attribute value starts with 'ix-rt-', then return the 2nd result.
If link text does not change, you shouldn't use xpath rather use link text:
driver.findElement(By.linkText("Being Powerful")).click();
However if you bend upon using xpath you may try following:
driver.findElement(By.xpath("//a[contains(#id,'ix-rt-')]")).click();
But this would select the 1st element containing 'ix-rt-' in its id. So it may not work as desired if there are more than 1 such elements. In that case, if you know the index of element on page, you may use following:
driver.findElement(By.xpath("(//a[contains(#id,'ix-rt-')])[2]")).click();

xpath search for divs where the id contains specific text

On my HTML page I have forty divs but I only want one div.
Using agility pack to search and get all the divs with Ids I use this XPath:
"//div[#id]"
But how do I search for divs with Ids where the id contains the text "test" like so:
<div id="outerdivtest1></div>"
Use the contains function:
//div[contains(#id,'test')]
I've used this with for the CSS class:
//div[#class = 'atom']
I assume it's similar with id's.
You can use the xpath
//div[#contains(#id,'test')]
If you want to use the first occurrence, it works fine but if it's not the first occurrence you have to go with different xpath specific to the particular element.
For example if you want first selection "id" is like that
(//[#id="numberIdentify"])//following:://a[contains(text(),'V ')]

Resources