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?

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 Dec 10 '13 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.

  • This does not really answer the OP's question, which is asking for the way to pass the variable using Python. – Kate Paulk Aug 24 '16 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.