简体   繁体   English

在Selenium 2 WebDriver(python)中遍历表

[英]Iterate through table in Selenium 2 WebDriver (python)

I have the following table layout in HTML (which changes accordingly): 我在HTML中有以下表格布局(相应地更改):

<table class="other-table-style">

    <tr> 
      <th>First Name</th>
      <th>Last Name</th>
      <th>Age</th>
    </tr>
    <tr>
      <td align="center" width="30%">Joe</td>
      <td align="center" width="30%">Bloggs</td>
      <td align="center" width="40%">28</td>
    </tr>
    <tr>
      <td align="center" width="30%">John</td>
      <td align="center" width="30%">Doe</td>
      <td align="center" width="40%">30</td>
    </tr>

</table>

I want to be able to iterate through this using Selenium 2.0 WebDriver, but I have not been able to find any good examples. 我希望能够使用Selenium 2.0 WebDriver进行迭代,但我找不到任何好的例子。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Used: 用过的:

from selenium.webdriver.common.by import By

trs = driver.find_elements(By.TAG_NAME, "tr") 

tds = trs[1].find_elements(By.TAG_NAME, "td")

This allows looping through each one to do as desired. 这允许循环遍历每一个以根据需要进行。

It seems like the person who posted this related question had code that would get you on the right track: 发布此相关问题的人似乎拥有可以让您走上正确轨道的代码:

for (WebElement trElement : tr_collection) {
    List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
    System.out.println("NUMBER OF COLUMNS = " + td_collection.size());
    col_num = 1;          

    if (!td_collection.isEmpty() && td_collection.size() != 1 ) {  
        for (WebElement tdElement : td_collection) {
            System.out.println("Node Name=== " + tdElement.getAttribute("class")); 
            System.out.println("Node Value=== " + tdElement.getText());
            col_num++;
        }
    }

    row_num++;
}

Edit: I changed their code somewhat... they were accumulating the class of each td and the text it contained in a hashmap and then once they had gone through the entire table, adding that into a master hashmap. 编辑:我稍微更改了他们的代码......他们正在累积每个td的类和它包含在hashmap中的文本,然后一旦他们完成整个表,就将其添加到主hashmap中。 Also this is the Java variant of Selenium so you would have to port it over. 这也是Selenium的Java变种,因此您必须将其移植。 The guts of it remain the same though - perhaps someone with more Selenium experience could give more info... I prefer to live over in WATIR land myself. 它的内容保持不变 - 也许拥有更多Selenium经验的人可以提供更多信息......我更喜欢自己住在WATIR土地上。

Here is an excellent tutorial for parsing tables (and links) using selenium . 这是一个使用selenium解析表(和链接)的优秀教程 Though it is written in Java, the translation into Python is quite trivial. 尽管它是用Java编写的,但对Python的翻译却相当简单。

for instance, to read row 2 column 2 of a table with the Python version of Selenium (2.41): 例如,使用Python版本的Selenium(2.41)读取表的第2行第2列:

from selenium import webdriver
driver = webdriver.Ie()

# assuming you're connected to your web page of interest, do:
x = driver.find_element_by_xpath('//table/tbody/tr[2]/td[2]')

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

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