Marc Adler
Marc Adler

Reputation: 534

Selenium - select an element in a table after another element

In a table in a webpage there are several plain text elements (filenames) on the left and several hyperlinks to PDFs of the files:

Red report     Download PDF
Blue report    Download PDF
Green report   Download PDF

There are several such pages, and the files aren't always in the same order.

Red report     Download PDF
Green report   Download PDF
Blue report    Download PDF

etc.

I only ever need the pdf for Green report. Finding the text Green report in the page using find_element is easy. What I don't know how to do is to focus the driver on the link to the right of the text. The xpath doesn't work, because it varies from page to page, and unfortunately the title of the pdf in the href doesn't have "green report" or anything like that in it—it's just a bunch of numbers.

However, the link element always follows the filename element in the html. What I need to do is tell selenium: "find the text Green report and then click on the first link appearing in the html after that text."

I've looked high and low and don't even know how to go about doing this, so I can't offer code snippets of what I've tried.

Here's an actual example:

<tr id="detailMainForm:j_idt922:0" class="rf-dt-r rf-dt-fst-r oddRow"><td id="detailMainForm:j_idt922:0:j_idt924" class="rf-dt-c" style="width:10%;">09.07.2015</td><td id="detailMainForm:j_idt922:0:j_idt927" class="rf-dt-c" style="width:50%;">Translation of the ISR</td><td id="detailMainForm:j_idt922:0:j_idt930" class="rf-dt-c" style="width:15%;"><a href="/search/docservicepdf_pct/id00000029896067/ETISR/WO2015102081.pdf">PDF (1p.)</a></td><td id="detailMainForm:j_idt922:0:j_idt935" class="rf-dt-c" style="width:25%;"><a href="/search/docservicepdf_pct/id00000029896067/ETISR/WO2015102081.pdf?download">PDF (1p.)</a>, <a href="/search/docservicepct_file/WOid00000029896067/ETISR/WO2015102081.zip">ZIP(XML + TIFFs)</a></td></tr>

Translation of the ISR is the text corresponding to "Green report" in my example above.

The first instance (there happen to be two--sometimes there are more) of PDF (1p.) corresponds to "Download PDF."

Upvotes: 1

Views: 73

Answers (1)

Florent B.
Florent B.

Reputation: 42528

This XPath returns the link element having a .pdf href for the row containing the text "Translation of the ISR" :

//tr[td='Translation of the ISR']//a[contains(@href, '.pdf')]

Upvotes: 1

Related Questions