Skip to content Skip to sidebar Skip to footer

Add Variable In Xpath In Python

While doing automation, I have to check that email which I am entering is correct one or not. Unfortunately, while creating XPath, UIAutomator is showing me the text of my email ad

Solution 1:

if are talking about an input maybe an xpath like this

xpath='//input[text()="'+email'"]'

give it a go :)

Solution 2:

The Xpath language normally lets you define variables in the expression's static context (this has been a thing since XPath 1.0), which some xpath libraries support:

>>> expr = "//*[local-name() = $name]"
>>> print(root.xpath(expr, name = "foo")[0].tag)
foo

>>> print(root.xpath(expr, name = "bar")[0].tag)
bar

(this is the lxml python library, based on libxml2, which only supports XPath 1.0)

Unfortunately, Selenium's use of XPath only goes as far as the browser's internal XPath engine does, and unfortunately, the DOM API for XPath does not expose these functionalities. You'll have to stick with the old string concatenation trick and remembering to escape all those variables to make sure they don't break your xpath expression.

Post a Comment for "Add Variable In Xpath In Python"