如何使用 selenium 和 python 动态读取 table 中的特定单元格值

how to dynamically read a specific cell value in a table using selenium and python

我正在编写一个自动化脚本 [使用 selenium 和 python],它应该执行以下操作

  1. 动态读取 table 行和列,查找在任何行中设置了 0 值的列[这是常量],如果找到,请单击 [assign/unassign] 按钮列同一行

我不想对值为“0”的列的 xpath 进行硬编码,而是动态查找它并继续。

下面的代码是我写的

trows = table1.find_elements_by_xpath("//table[@id='ambassadors-for-assignment']/tbody/tr")
row_count = len(trows)
tcols = trows.find_elements_by_xpath("//table[@id='ambassadors-for-assignment']/tbody/tr/td")
col_count = len(tcols)
first_part = "//table[@id=ambassadors-for-assignment']/tbody/tr["
second_part = "]/td["
third_part = "]"
for i in range(1, len(row_count)):
    for j in range(1, len(col_count)):
          final_xpath = first_part+i+second_part+j+third_part      

HTML 文件结构

<tbody>
  <tr>
    <td> james </td>
    <td> watson </td>
    <td> 10 | 5 </td>
    <td>
      <div class="btn-group" role="group">
         <button class="btn btn-success" type="button">
             <i class="fa fa-plus"></i>
         </button>
        <button class="btn btn-danger" type="button">
            <i class="fa fa-minus"></i>
        </button>
      </div>
    </td>
  </tr>
....

我的 HTML 文件有 n 行,列数如上。正如我提到的,我想阅读第三列值[即 10 | 5] 看它是否为 0 [仅考虑第三列中的第一项],然后单击下一列中的按钮[btn btn-success]。

任何进一步进行的指示将不胜感激!

我会在评论部分提供 [​​=25=] 到实际 HTML 文件

I do not want to hard-code the xpath of the column that has value "0"

from selenium import webdriver
import re

driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550) #For bug
driver.get("http://localhost:8000")

pattern = r"""
    \s*         #Match whitespace, 0 or more times, followed by...
    (\d+)       #a digit, one or more times, captured, followed by
    \s*         #whitespace, 0 or more times, followed by...
    [|]         #vertical bar, followed by...
    \s*         #whitespace, 0 or more times, followed by...
    \d+         #a digit, one or more times
"""
regex = re.compile(pattern, re.X)

table = driver.find_element_by_id('ambassadors-for-assignment')
trs = table.find_elements_by_tag_name('tr')

for tr in trs:
    tds = tr.find_elements_by_tag_name('td')

    for td in tds:
        match_obj = re.search(regex, text)

        if match_obj and match_obj.group(1) == '0':
            success_button = tr.find_element_by_css_selector('button.btn-success')
            print success_button.get_attribute('type')
            success_button.click()

re.match(模式、字符串、标志=0)
如果字符串开头的零个或多个字符匹配正则表达式模式,return 一个对应的匹配对象。 Return None 如果字符串与模式不匹配;请注意,这与零长度匹配不同。

请注意,即使在 MULTILINE 模式下,re.match() 也只会匹配字符串的开头,而不是每行的开头。

如果您想在字符串中的任何位置找到匹配项,请改用 search()(另请参阅 search() 与 match())。

https://docs.python.org/3/library/re.html#module-re

======

这里是 xpath,我认为它更符合你正在尝试做的事情,即给定一列,查看值 0 的行:

from selenium import webdriver
import re

driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550) #For bug
driver.get("http://localhost:8000")

pattern = r""" 
    \s*         #Match whitespace, 0 or more times, followed by...
    (\d+)       #a digit, one or more times, captured, followed by
    \s*         #whitespace, 0 or more times, followed by...
    [|]         #vertical bar, followed by...
    \s*         #whitespace, 0 or more times, followed by...
    \d+         #a digit, one or more times
"""
regex = re.compile(pattern, re.X)

trs = driver.find_elements_by_xpath('//table[@id="ambassadors-for-assignment"]/tbody/tr')
target_columns = [3, 4]

for target_column in target_columns:
    for tr in trs:
        target_column_xpath = './td[{}]'.format(target_column)  #VARY COLUMN HERE ***
        td = tr.find_element_by_xpath(target_column_xpath)
        match_obj = re.match(regex, td.text)

        if match_obj and match_obj.group(1) == '0':
            button_xpath = './/button[contains(concat(" ", normalize-space(@class), " "), " btn-success ")]' 
            success_button = tr.find_element_by_xpath(button_xpath)
            #success_button.click()

            print "column {}:".format(target_column)
            print match_obj.group(0)
            print success_button.get_attribute('class')
            print

输出将如下所示,具体取决于您尝试使用正则表达式匹配的文本:

column 3:
0 | 5
btn btn-success

column 4:
0 | 61
btn btn-success

但在我看来,必须在 xpath 中使用以下内容:

'[contains(concat(" ", normalize-space(@class), " "), " btn-success ")]'

来匹配 class,意味着使用 xpath 不是实现它的方法。 python 方法:

find_element_by_csss_selector('button.btn-success')

...将做同样的事情更简洁明了。