I need to click a checkbox on a table by serial number but the serial number isn't tied into the checkbox by any unique identifier:
Here's the serial:
<a href="#" style="padding-left:10px;" id="inactive_406906" onclick="getDeviceInfo(406906); return false;"> SZK190052 </a>
Here's the checkbox:
<input id="inactiveDeviceSelectionCheckbox" name="inactiveList" type="checkbox" value="406906" onclick="onClickDeviceCheckbox(this, document.myform.deviceInactiveCheckAll, 406906, false, false, false, false, false, 0)" onchange="populateInactiveCheckedDevice(this, 406906, false, false)">
Here's my attempt:
browser.find_element_by_xpath("//tr[.//a[text()='SSJD90134']]//input[@class='deviceSelectionCheckBox']").click()
I get the following error:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//tr[.//a[text()='SSJD90134']]//input[@class='deviceSelectionCheckBox']"}
Unfortunately its not working, any help is appreciated.
Thanks,
CodePudding user response:
Instead of text()='SSJD90134'
you can use contains(.,'SSJD90134')
so that instead of
browser.find_element_by_xpath("//tr[.//a[text()='SSJD90134']]//input[@class='deviceSelectionCheckBox']").click()
You can use
browser.find_element_by_xpath("//tr[.//a[contains(.,'SSJD90134')]]//input[@class='deviceSelectionCheckBox']").click()
Or, as mentioned by Kunduk you can remove spaces with normalize-space()
method
browser.find_element_by_xpath("//tr[.//a[normalize-space() = 'SSJD90134']]//input[@class='deviceSelectionCheckBox']").click()
CodePudding user response:
You are nearly there. There is space between your text you are searching. Use below xpath.
//tr[.//a[normalize-space(text())='SSJD90134']]//input[@class='deviceSelectionCheckBox']
so your code be like
browser.find_element_by_xpath("//tr[.//a[normalize-space(text())='SSJD90134']]//input[@class='deviceSelectionCheckBox']
").click()
One more Xpath option:
//td[.//a[normalize-space(text())='SSJD90134']]/preceding-sibling::td//input[@class='deviceSelectionCheckBox']
CodePudding user response:
There are spaces around the link test:
driver.find_element(By.XPATH, "//*[matches(text(),'SZK190052']//input").click()
or
driver.find_element(By.XPATH, "//*[text()=' SZK190052 ']//input").click()