I am doing a selenium automation where I want to pass the variable into the XPath and select the element.
The xpath which I am trying looks like:
browser.find_element_by_xpath("//span[contains(.,'SEM W880 Black')]")
I would like to pass the text SEM W880 Black
as a variable to the XPath. How can I do it using python 3?
2 Answers 2
At first glance, this appears to be a Python question rather than a QA question. The answer to the Python question is something like,
x = 'SEM W880 Black'
browser.find_element_by_xpath("//span[contains(.,'" + x + "')]")
If I misunderstood you, please update the question.
-
This is neat answer,I have not thought we can write in python. I thought there could be a function in the selenium where I can pass the variable as a parameter to the function.Ranadheer– Ranadheer2013年12月10日 09:27:07 +00:00Commented Dec 10, 2013 at 9:27
If you are using Selenium IDE, you can do this:
Declaration in Selenium IDE:
<tr>
<td>storeExpression</td>
<td>SEM W880 Black</td>
<td>name_of_your_variable</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//a[contains(text(),'${name_of_your_variable}')]</td>
<td></td>
</tr>
Simply put, you can store the variable and use it between simple quotes.
Please take note that the target field, in Selenium IDE is without any kind of quotes.
-
This does not really answer the OP's question, which is asking for the way to pass the variable using Python.Kate Paulk– Kate Paulk2016年08月24日 18:27:13 +00:00Commented Aug 24, 2016 at 18:27