The element to match is as follows:
... some html....
<table>
some <tr's>
<tr><td>"caption in a <a> tag"</td><td> result value here </td></tr>
more <tr's>
.... more html
I'm using Selenium IDE but don't see a way to match/capture the result value text anywhere ?
Given
<yyy name="iggy" caption="abcd" level="20"></yyy>
you would want to use
assertAttribute("iggy#caption", "abcd")
This uses the name as a locator and #caption to specify the attribute you want. You could follow it with
assertAttribute("iggy#level", "20")
to check the level attribute. You might use 'validateAttribute' instead of 'assertAttribute'. If you want to capture the attribute value use 'storeAttribute'.
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 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 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]
My Problem:
The Page I try to test with NightwatchJS Contains Some Input Fields that have the Same beginning, but a random number is added. I want to Fill the Textfield on the page. Only one with this name is present on the same time.
<input id="groupNamec7aed06a-67a1-4780-9cc3-5b985666adb9" class="d-styled-input" data-value-update="keyup" data-bind="value: name" title="">
Is the definition of the Field. groupName is every Time the same, but the number changes.
Is there a possibility to use CSS Selector in nightwatch instead of XPATH?
You can try this way :
input[id^="groupName"]
From MDN > Attribute selectors :
[attr^=value] : Represents an element with an attribute name of attr and whose first value is prefixed by "value".
Unfortunately CSS Selector does not provide such a way. You could use a different CSS Selector to match inputs with an id and get those as a list. Afterwards using getAttribute('id') you could do it manually, but this seems like unnecessary effort to me and I'd recommend just using Xpath.
Ofcourse you could try and get a different unique CSS Selector. If it's in a form you could locate the form and use :nth-child but if I remember correctly this has limited/no support in IE.
Edit Apparently IE9 and later does support :nth-child
I have this code for xpath and html:
<a class="WatchButton inicon" rel="nofollow" data-productid="111124">
xpath=/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a
How can I get the data-productid value?
Just add #data-productid to the xpath expression:
/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a/#data-productid
Note that the xpath expression you have is very fragile since it depends on a bunch of elements and their relevant positions. Try to rely on the element's attributes or one of it's containers - look for id and class attributes. For example:
//a[contains(#class, "WatchButton")]/#data-productid
This gets the first link anywhere on a page that contains WatchButton class and retrieves it's data-productid attribute value.
* Sharing the link to the web page or showing the complete HTML could help to provide you with a more reliable xpath expression.