user15036284
user15036284

Reputation:

python selenium search for exact text in web table

I am struggling to find syntax when trying to locate exact text in web table with Selenium and Python.

Table xpath is:

driver.find_element_by_xpath('//*[@id="someId"]/table[2]')

There are 4 columns and unknown number of rows. I am interested only in First column. Example xpath of 3 rows:

//*[@id="sameValue"]/table[2]/tbody/tr[3]/td[1]/input
//*[@id="sameValue"]/table[2]/tbody/tr[4]/td[1]/input
//*[@id="sameValue"]/table[2]/tbody/tr[5]/td[1]/input

Basically, I have radio buttons in first column and I want to click on the row in First column that has MY VALUE. I don`t know if it will be 4th or 26th row. I know it is the First column. Some additional details:

<td class="class value" width="70">
<input type="radio" name="FileName" value="some Value">
</td>

<td class="class value" width="70">
<input type="radio" name="FileName" value="some Value">
</td>

<td class="class value" width="70">
<input type="radio" name="FileName" value="some Value">
</td>

<td class="class value" width="70">
<input type="radio" name="FileName" value="MY VALUE">
</td>

And one more thing the name I am looking for comes as variable. I guess syntax will be some F -string, so please give me hints about that too, if you can.

Thank you for your time, knowledge and effort. They are much appreciated

Upvotes: 1

Views: 1143

Answers (2)

vitaliis
vitaliis

Reputation: 4212

Try xpath locating by value text:

driver.find_element_by_xpath("//input[@value='MY VALUE']")

Upvotes: 0

KunduK
KunduK

Reputation: 33384

To click on the input element with value attribute as value="MY VALUE" use the either of the following xpath.

//*[@id='sameValue']/table[2]/tbody//tr//td[1]/input[@value='MY VALUE']

Or

//input[@value='MY VALUE']

In python you could use the python format() function.

strvar="MY VALUE"
driver.find_element_by_xpath("//*[@id='sameValue']/table[2]/tbody//tr//td[1]/input[@value='{}']".format(strvar)).click()

Upvotes: 1

Related Questions