简体   繁体   中英

Python Selenium click on a specific row in a table containing the right data in a column

So I have a table that can have from 0 to x rows and always have 7 columns.

Something like below.

    Type   Price   Store  Weight  For-sale  Stock  Discount
x
x
x
x
x

here is how the HTML looks:

 <table id="my_table" class="datatable" cellspacing="0" cellpadding="0" border="0" style="border-width:0px;border-collapse:collapse;"> <tbody> <tr> <tr class="row" style="cursor:pointer;" onclick="javascript:__doPostBack('my$table','Select$0')"> <td> <td class="first">Meat</td> <td>75</td> <td>Adams grocery</td> <td align="center">1kg</td> <td>Yes</td> <td>Full</td> <td>Yes</td> <td> </tr> <tr class="row" style="cursor:pointer;" onclick="javascript:__doPostBack('my$table','Select$1')"> <td> <td class="first">Vegetable</td> <td>25</td> <td>Adams grocery</td> <td align="center">0.5kg</td> <td>No</td> <td>Empty</td> <td>No</td> <td> </tr> </tbody> </table> </div> 

What I want to do is to click on each row if exists that contains the text "Adams grocery" (which is in column 3) so they open in a separate tab, then give new instructions to all tabs at once. For example: Click button "welcome" on all tabs. I have a feeling the above might be a little too complicated for me as a beginner... So I thought maybe just click on one of the rows to begin with.

Been thinking about this the whole day, thanks for all help!

Do you need something like this:

Tested to this html: http://jsfiddle.net/zvhrm6tf/

from selenium.webdriver.support.wait import WebDriverWait    

td_list = WebDriverWait(driver, 10).until(lambda driver: driver.find_elements_by_css_selector("#my_table tr td"))
for td in td_list:
    if(td.text == "Adams grocery"):
        td.click()

and if you need to target the table row you could do something like this:

tr = td.find_element_by_xpath("..")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM