简体   繁体   English

Python Selenium单击表中包含正确数据的特定行

[英]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. 所以我有一个表,可以有0到x行,并且总是有7列。

Something like below. 像下面这样。

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

here is how the HTML looks: HTML的外观如下:

 <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. 我想做的是单击每一行(如果存在的话),其中包含文本“ Adams杂货店”(在第3列中),以便它们在单独的标签中打开,然后立即为所有标签提供新说明。 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/ 经过测试的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("..")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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