Get text of td following the second occurrence of an element in Selenium using Python -
i'm trying find text after remarks field in form. however, table has multiple remarks fields. want able grab text in td follows td of second remarks field. have following html:
<table> <tbody> <tr>...</tr> <tr>...</tr> <tr> <td>remarks:</td> <td>this first remarks field </tr> <tr> <td>anotherfield:</td> <td>content of field</td> </tr> <tr> <td>remarks:</td> <td>this second remarks field</td> </tr> <tr>...</tr> </tbody> </table>
to grab text out of first remarks field, can following:
ret = driver.find_element_by_xpath("//td[contains(text(),'remarks')]/following::td") print ret.text
however, need grab content out of second remarks field. has done based on index of occurrences of 'remarks', not based on index. i've wanted try things this:
ret = self.driver.find_element_by_xpath("//td[contains(text(),'remarks')][1]/following::td")
or:
rets = self.driver.find_elements_by_xpath("//td[contains(text(),'remarks')]")[1] ret = elements.find_element_by_xpath("/following::td")
understandingly, these not work. there way of doing this? using command along lines of 'the field after nth occurrence of remarks' i'm looking for.
p.s. have done using xpath. reason being, i'm trying convert coworkers code selenium application has revolved around xpath.
i'm using selenium-2.44.0 , python 2.7.
indexing starts 1 in xpath:
(//td[contains(., 'remarks')]/following-sibling::td)[2]
or, can use find_elements_by_xpath()
, second item:
elements = self.driver.find_elements_by_xpath("//td[contains(., 'remarks')]/following-sibling::td") print elements[1].text
Comments
Post a Comment