Get value of aggregate xpath function in Selenium Java - xpath

I'm trying to get the index of a <tr> element based on the contents of its <td> elements in Selenium using xpath.
String xpath = "count(//tr[td[text()='Column Value A'] and td[text()='Column Value B']]/preceding-sibling::*)"
WebElement count = driver.findElement(By.xpath(xpath))
However, I'm getting this exception.
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression count(//tr[td[text()='Column Value A'] and td[text()='Column Value B']]/preceding-sibling::*) because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.
Is there any way for Selenium to return the value of an aggregate xpath function?
Thanks in advance for your help!

Related

I'm trying to get the "id" of an html tag, but the xpath doesnt work

I'm using selenium and i want to get the "id" of an html tag with "find_elements_by_xpath", but i've got this error :
selenium.common.exceptions.InvalidSelectorException: Message: invalid
selector: The result of the xpath expression
"//body[contains(#class,'de')]/div/div[contains(#class,'container-fluid
default')]/section[contains(#id,'mainContent')]/div[contains(#class,'row-fluid')]/div[contains(#id,'contentContainer
row-fluid')]/div[contains(#class,'content')]/div[contains(#class,'ses')]/ul/li/#id"
is: [object Attr]. It should be an element.
When i executed this code:
browser.find_elements_by_xpath("//body[contains(#class,'de')]/div/div[contains(#class,'container-fluid default')]/section[contains(#id,'mainContent')]/div[contains(#class,'row-fluid')]/div[contains(#id,'contentContainer row-fluid')]/div[contains(#class,'content')]/div[contains(#class,'ses')]/ul/li/#id")
While the same code without "/#id" work perfectly but i've got only the text in the "li" tag and it's not what i want.
According to the error, the problem comes from the Xpath.
I expected that this code would return all the "id" that are in "li" html tag, but i got the error.
Thank you for your help
#id is an attribute, not an element. The XPath is OK, but the function only returns elements, not attributes. I doubt there's find_attributes_by_xpath, but if you want to find the li element that has the #id defined, you can specify that in the quantifier:
browser.find_elements_by_xpath("//body[contains(#class,'de')]
/div/div[contains(#class,'container-fluid default')]
/section[contains(#id,'mainContent')]
/div[contains(#class,'row-fluid')]
/div[contains(#id,'contentContainer row-fluid')]
/div[contains(#class,'content')]
/div[contains(#class,'ses')]/ul/li[#id]")
~~~~~
You can then call element.get_attribute('id') to retrieve the id of the element.

How to get value from Node in Selenium Webdriver and Print in Console?

I have tried using Webelement to store value but
facing Error like :
[Below is the image of which I want to get value that is K1]
I am Not Getting value through Weblelement as It is displaying Error as
org.openqa.selenium.InvalidSelectorException: The given selector //div[2]/label/strong/following-sibling::text()
is either invalid or does not result in a WebElement.
I just want to store value in variable and print it, I have tried below code but no result :
WebElement Key = driver.findElement(By.xpath("//div[2]/label/strong/following-sibling::text()"));
String vs =Key.getText();
System.out.println("key Name..!!!" + vs);
Suggest Me Opinions
Try with this Xpath to obtain the WebElement:
.//strong[text()='Key']/../..//label
After that with element.getText() you should have the value : K1

InvalidSelectorException while trying to identify web element in robot framework

ign-In | FAIL |
InvalidSelectorException: Message: The given selector xpath=//*[#id='user_email'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression xpath=//*[#id='user_email'] because of the following error:
TypeError: The expression cannot be converted to return the specified type.
Stacktrace:
at FirefoxDriver.annotateInvalidSelectorError_ (file:///var/folders/5f/6mvs5x1j37s5q3_38kjfwkgr0000gn/T/tmp1Nrpuj/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/driver-component.js:10633)
at FirefoxDriver.prototype.findElementsInternal_ (file:///var/folders/5f/6mvs5x1j37s5q3_38kjfwkgr0000gn/T/tmp1Nrpuj/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/driver-component.js:10691)
at FirefoxDriver.prototype.findElements (file:///var/folders/5f/6mvs5x1j37s5q3_38kjfwkgr0000gn/T/tmp1Nrpuj/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/driver-component.js:10695)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/5f/6mvs5x1j37s5q3_38kjfwkgr0000gn/T/tmp1Nrpuj/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/5f/6mvs5x1j37s5q3_38kjfwkgr0000gn/T/tmp1Nrpuj/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///var/folders/5f/6mvs5x1j37s5q3_38kjfwkgr0000gn/T/tmp1Nrpuj/webdriver-py-profilecopy/extensions/fxdriver#googlecode.com/components/command-processor.js:12481)
This part of the error message explained why :
The given selector xpath=//*[#id='user_email'] is either invalid or does not result in a WebElement.
In this case, //*[#id='user_email'] is valid XPath expression, so it must be no element matched by that XPath. That's all I can tell from the information provided.

XPath Get value inside query string

I use this XPath
//#getparam
to get this list of URL
/blabla/bla.php?id=100001&target=lala&new=lulu
/blabla/bla.php?id=100002&target=lala&new=lulu
/blabla/bla.php?id=100003&target=lala&new=lulu
I want to get list of IDs, and try this XPath
substring-before(substring-after(//#getparam, "?id="), "&")
but it only returns first ID. How to get all IDs only using XPath?
If you're using XPath 2.0, the following will do the trick:
//#getparam/substring-before(substring-after(., "?id="), "&")
If you're using XPath 1.0, there is no single expression to return the desired result set. You can iterate over each result of //#getParam and apply the functions.

How value will be choose in xpath

<DocumentList>
<DocumentId>node1175251</DocumentId>
<DocumentId>node1175254</DocumentId>
<DocumentId>node1175385</DocumentId>
</DocumentList>
I have this Xml.
I want to select the value of DocumentId.
I need xpath for select the value. Anyone Help me?
I want to select the value of DocumentId. I need xpath for select the
value. Anyone Help me?
This selects all DocumentId elements:
/*/DocumentId
This selects the first DocumentId element:
/*/DocumentId[1]
This selects the second DocumentId element:
/*/DocumentId[2]
This selects the last DocumentId element:
/*/DocumentId[last()]
All of the above XPath expression select nodes (elements).
An XPath expression doesn't select a value. It may produce a value. Also, what you are actually asking for is the string value of a particular DocumentId element:
This Xpath expression produces the string value of the first DocumentId element:
string(/*/DocumentId[1])
This Xpath expression produces the string value of the second DocumentId element:
string(/*/DocumentId[2])
This Xpath expression produces the string value of the last DocumentId element:
string(/*/DocumentId[last()])
/DocumentList/DocumentId
/DocumentList/DocumentId[1] // for the first instance
/DocumentList/DocumentId[last()] // for the last instance
Also, /DocumentList/ would ensure that /DocumentId[1] is inside DocumentList tag
use .eq( index ) java script function
to get the value of xml node using index as parameter

Resources