【问题标题】:Marking off checkboxes in a table that match a specific text标记表格中与特定文本匹配的复选框
【发布时间】:2017-03-19 06:34:41
【问题描述】:

我有一个表格(下面的屏幕截图),我想在其中使用 selenium python 标记在同一行中具有文本“Xatu Auto Test”的复选框。

我试过关注这两个帖子:

但我无法让这些解决方案在我的代码上运行。

我的代码:

form = self.browser.find_element_by_id("quotes-form")

try:
    rows = form.find_elements_by_tag_name("tr")
        for row in rows:
            columns = row.find_elements_by_tag_name("td")
            for column in columns:
                if column.text == self.group_name:
                    column.find_element_by_name("quote_id").click()
except NoSuchElementException:
    pass

复选框从未被点击,我想知道我做错了什么。

这是我使用 FirePath 检查时的 HTML:

<form id="quotes-form" action="/admin/quote/delete_multiple" method="post" name="quotesForm">
    <table class="table table-striped table-shadow">
        <thead>
        <tbody id="quote-rows">
        <tr>
        <tr>
            <td class="document-column">
            <td>47</td>
            <td class="nobr">
            <td class="nobr">
            <td class="nobr">
            <td class="nobr">
                <a title="Xatu Auto Test Data: No" href="http://192.168.56.10:5001/admin/quote/47/">Xatu Auto Test</a>
            </td>
            <td>$100,000</td>
            <td style="text-align: right;">1,000</td>
            <td class="nobr">Processing...</td>
            <td class="nobr">192.168....</td>
            <td/>
            <td>
                <input type="checkbox" value="47" name="quote_id"/>
            </td>
        </tr>
        <tr>
    </tbody>
    <tbody id="quote-rows-footer">
</table>
<div class="btn-toolbar" style="text-align:center; width:100%;">

【问题讨论】:

  • 如果没有看到完整的代码,很难理解。你确定if column.text == self.group_name:比较正确吗?
  • 这是我在这种情况下尝试做的完整代码。哦,对不起,我没有解释“self.group_name”。您可以将其替换为“Xatu Auto Test”。
  • 是的,我相信比较是正确的。如果这就是你的意思,我没有收到任何语法错误。

标签: python selenium checkbox html-table row


【解决方案1】:

快速浏览一下,当您尝试访问columnquote_id 时,我认为这条线需要更改,它应该是row 的:

发件人:

column.find_element_by_name("quote_id").click()

收件人:

row.find_element_by_name("quote_id").click()

附:只要像@Saifur 评论的那样,你的比较就正确了。

更新:

我已经运行了一个模拟,如果将column 更改为row,则确实勾选了该复选框,简化版:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('your-form-sample.html')
form = driver.find_element_by_id("quotes-form")

rows = form.find_elements_by_tag_name("tr")
for row in rows:
    columns = row.find_elements_by_tag_name("td")
    for column in columns:
        # I changed this to the actual string provided your comparison is correct
        if column.text == 'Xatu Auto Test':
            # you need to change from column to row, and it will work
            row.find_element_by_name("quote_id").click()

这是输出:

【讨论】:

  • 感谢您的回复,这很有帮助!虽然 .click() 对我不起作用,这很奇怪。当我将其更改为 .send_keys(Keys.SPACE) 时,它就起作用了。
  • @AlphaFoxtrot,很高兴它有帮助。奇怪,我用的是 Firefox 驱动,你用的是 Chrome 吗?
  • 我也在使用 Firefox 驱动程序。这可能是由于我正在测试的应用程序。但是,单击对同一域中其他页面上的其他元素有效。
猜你喜欢
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2023-03-27
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多