在表中标记与特定文本匹配的复选框

2024-09-30 02:26:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表(下面的屏幕截图),我想用seleniumpython标记出在同一行中有文本“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%;">


Tags: nameinformidbytablecolumnfind
1条回答
网友
1楼 · 发布于 2024-09-30 02:26:06

通过快速查看,我认为这行需要更改,因为您试图访问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()

输出如下:

enter image description here

相关问题 更多 >

    热门问题