1

I have following dynamic webtable

(checkbox)   ID_No  country_code  Date          Time           UserName          FileName
            1       5           10/04/2019    12:05:45       [email protected]      filename1
            2       7           10/04/2019    13:05:45       [email protected]      filename2
            3       8           10/03/2019    14:05:45       [email protected]      filename2
            4       9           10/04/2019    14:08:45       [email protected]      filename1

On the left hand side of the id_no there is a checkbox.I only want to check the rows where FileName='filename1' and Date='10/04/2019'

when I use this xpath in the chrome console: $x("//tr[td[contains(text(),'filename1')]]")[0]'

This contains 1st row that contains "filename1" value.I get following HTML for the rows of this webtable

<tr class="class1">
  <td width="2%"> </td>   <!–– checkbox ––>
  <td width="2%"> 1 </td>
  <td width="2%"> 5 </td>
  <td width="2%"> 10/04/2019 </td>
  <td width="2%"> 12:05:45 </td>
  <td width="2%" style=word-wrap:break-word> [email protected] </td>
  <td width="2%"> filename1 </td>
</tr>

To check the checkboxes of rows that contains "filename1" I am using following code

driver = webdriver.Chrome() 
driver.get(website_URL) 
driver.find_element_by_xpath("//tr[td[contains(text(),'filename1')]]/input").click()

How can I check the checkboxes where FileName='filename1'and Date='10/04/2019'?

Also,How can I uncheck the checkboxes?

4 Answers 4

2

You can uncheck the boxes the same way that you check them -- by clicking the input element.

The XPath locator you provided is a good one. To select other input elements underneath the td's, you just need to change the text:

driver.find_element_by_xpath("//tr[td[contains(text(),'10/04/2019')]]/input").click()

To find the input with both, you can use this:

driver.find_element_by_xpath("//tr[td[contains(text(),'filename1')] and td[contains(text(),'10/04/2019')]]/input").click()

Hope this helps a bit.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks,but I am not getting it correct when I use driver.find_element_by_xpath("//tr[td[(contains(text(),'filename1')) and (contains(text(),'10/04/2019'))]]/input").click()
You are looking for text in the same two td elements here, but filename1 and 10/04/2019 are in separate td elements here. This would be the correct path to look for tr that has td elements with filename1 and 10/04/2019: //tr[td[contains(text(),'filename1')] and td[contains(text(),'10/04/2019')]]/input
2

The below code will check and uncheck the checkboxes where FileName='filename1' and Date='10/04/2019'.

driver.find_element_by_xpath("//tr[td[contains(text(),'10/04/2019')] and td[contains(text(),'filename1')] ]//input").click()

Comments

1

The HTML you posted is missing the INPUT that you want to click. Given your description of the table, I'm assuming it's inside the first TD in the row as I've shown below.

<tr class="class1">
  <td width="2%"> <input type="checkbox"> </td>   <!–– checkbox ––>
  <td width="2%"> 1 </td>
  <td width="2%"> 5 </td>
  <td width="2%"> 10/04/2019 </td>
  <td width="2%"> 12:05:45 </td>
  <td width="2%" style=word-wrap:break-word> [email protected] </td>
  <td width="2%"> filename1 </td>
</tr>

If this is the case, the XPath below will work

//tr[./td[.=' filename1 ']][./td[.=' 10/04/2019 ']]/td/input

If you plan to reuse this locator with other filenames and dates, I would insert variables into the locator. There are many, many ways to insert a variable into a string using python. If you are using python 3.6, you can use the formatted string literals below, as an example.

//tr[./td[.=' {filename} ']][./td[.=' {date} ']]/td/input

Now you can create a method and pass in the filename and date and return the correct INPUT easily.

Comments

-1

Could you try this? $x('//tr[td[contains(text(),"filename1")] and td[contains(text(),"10/04/2019")]]') Sorry, I misunderstood. I updated the answer.

Comments

Your Answer

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.