1

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?

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Nov 18, 2013 at 17:39

2 Answers 2

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.

answered Nov 19, 2013 at 0:16
1
  • 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. Commented Dec 10, 2013 at 9:27
0


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.

answered Aug 24, 2016 at 17:22
1
  • This does not really answer the OP's question, which is asking for the way to pass the variable using Python. Commented Aug 24, 2016 at 18:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.