Reputation: 133
I believe I am having a similar issue to the question Python & Selenium Clicking on td value in a row based on another td in the same row
Table layout (I've removed the column data for those that I am not interested in for clarity):
<tbody>
<tr class="FontWeightBold">
<td>Title ID</td>
<td>Primary Author Last Name</td>
<td>Title: Edition</td>
<td>Draft Due</td>
<td>Final Due</td>
<td>Est. Trans. Date</td>
<td>Target Pub. Date</td>
<td><td>
<td><td>
</tr>
<tr>
<td>
<span id="SmartMasterContent_TitleProposalContent_dgProposals_lBookID_0">T276065</span>
</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>
<a id="SmartMasterContent_TitleProposalContent_dgProposals_lbAccept_0" href="javascript:__doPostBack('ctl00$ctl00$SmartMasterContent$TitleProposalContent$dgProposals$ctl02$lbAccept','')">Request ISBN</a>
</td>
<td>..</td>
</tr>
<tr>
<td>
<span id="SmartMasterContent_TitleProposalContent_dgProposals_lBookID_1">T276066</span>
</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>..</td>
<td>
<a id="SmartMasterContent_TitleProposalContent_dgProposals_lbAccept_1" href="javascript:__doPostBack('ctl00$ctl00$SmartMasterContent$TitleProposalContent$dgProposals$ctl03$lbAccept','')">Request ISBN</a>
</td>
<td>..</td>
</tr>
Example problem. I want to click on the "Request ISBN" link for the table row where Title ID (first column) is "T276066".
One of the issues I am having is that the id for the link will have the row number appended to it and the record i am looking at could be in any position on the table.
My Python Code snippet
Request_ISBN = driver.find_element_by_xpath('//a[text()="T276066"]/parent::td/preceding-sibling::td/a[text() = "Request ISBN"]')
To try and reduce areas where errors could be generated I have hardcoded the Title ID into the XPath however this will be replaced with a variable once the code works.
What should be the xpath code?
Upvotes: 1
Views: 776
Reputation: 7401
What about finding span
with text T276066
then finding its ancestor tr
and then drill-down to the relevant td/a
, see the following:
driver.find_element_by_xpath("//span[text()='T276066']/ancestor::tr/td/a[text()='Request ISBN']").click()
Upvotes: 1