0

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 address. I want to make XPath dynamic, so, that every time, I use that XPath, It gets the text. Let us suppose:

email = 'testqatp@gmail.com'

and XPath is:

[@text='email']

How is that gonna work?

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
TeSter
  • 119
  • 1
  • 11
  • 1
    Do you mean `"//*[@text='{}']".format(email)`? – Andersson Jan 03 '18 at 18:36
  • 1
    Possible duplicate of [How to pass variable parameter into XPath expression?](https://stackoverflow.com/questions/30352671/how-to-pass-variable-parameter-into-xpath-expression) – kjhughes Jan 03 '18 at 18:38
  • Possible duplicate of [How do I put a variable inside a String in Python?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-inside-a-string-in-python) – Andersson Jan 03 '18 at 19:37

2 Answers2

0

if are talking about an input maybe an xpath like this

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

give it a go :)

0

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.

VLRoyrenn
  • 596
  • 5
  • 11