【问题标题】:how to dynamically read a specific cell value in a table using selenium and python如何使用 selenium 和 python 动态读取表中的特定单元格值
【发布时间】:2023-03-27 02:50:02
【问题描述】:

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

  1. 动态读取表格的行和列,查找在任何行中设置为 0 值的列[这是常量],如果找到,请单击同一行中的 [分配/取消分配] 按钮列

我不想对值为“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]。

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

我将在 cmets 部分提供指向实际 HTML 文件的链接

【问题讨论】:

  • 链接到 HTML : jsfiddle.net/gkfqa644/2
  • 没有一个 td 符合您的描述,即没有一个 td 看起来像 0 | 20

标签: python python-2.7 selenium selenium-webdriver


【解决方案1】:

我不想对值为“0”的列的 xpath 进行硬编码

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)
如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的匹配对象。如果字符串与模式不匹配,则返回 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 ")]'

匹配一个类,意味着使用 xpath 不是这样做的方法。 python方法:

find_element_by_csss_selector('button.btn-success')

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

【讨论】:

  • 从您的代码中,我了解到使用正则表达式将有助于进一步进行,感谢您指导我正确的方向。我没有想到 python 正则表达式会有所帮助!感谢你的帮助!我已将您的答案标记为正确,我是这里的新手,还没有投票的权限!
  • @KK16,不用担心。我发布了另一个示例,我认为它更符合您的尝试。
  • 也将通过第二个示例,看看哪一个将有助于更好地完成我的任务。感谢您的及时回复!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
相关资源
最近更新 更多