【问题标题】:Select radio button using Selenium with value that changes使用 Selenium 选择单选按钮,其值会发生变化
【发布时间】:2021-06-25 19:03:17
【问题描述】:

我需要用value="QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW"选择下面的单选按钮

HTML:

<form name="ViewQueryForm" method="post" action="/equery/getAttachments.do">

<div class="txt_align_left innerdvcont" id="tabmenu1" style="display:">

<div class="clear"></div>

<div class="txt_align_left innerdvcont" id="tabmenu011" name="tabmenu011">

        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <thead>
             <tr>
            <th width="10%" style="text-align: left"></th>
                    <th width="60%" style="text-align: left">Attachment </th>
                    <th width="30%" style="text-align: left">Date </th>
                    
            </tr>
                </thead>
        
                <tbody>
                <tr>
                <td align="center" valign="top">
                <input type="radio" name="getAttachmentValue" id="getAttachmentValue" value="Dispute_1466718.xlsx;SYPFJPC2MQHC-5-687433#QHBTW"> </td>
                    <td style="padding:5px 4px">Dispute_1466718.xlsx</td>
                    <td style="padding:5px 4px">2021-02-16T10:34:08.617</td>
                    
                        </tr>
                        
                </tbody><tbody>
                <tr>
                <td align="center" valign="top">
                <input type="radio" name="getAttachmentValue" id="getAttachmentValue" value="QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW">    </td>
                    <td style="padding:5px 4px">QMBT-0029104.xlsx</td>
                    <td style="padding:5px 4px">2021-03-27T08:08:46.09</td>
                    
                        </tr>
                        
                
            </tbody>    
            </table>    
</div>  

到目前为止,我已经能够使用下面的代码点击它:

radiobutton2 = driver.find_element_by_xpath("//input[@value='QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW']");
radiobutton2.click()

但是,该值每次都会更改,这意味着它不是我在运行代码时可以使用的东西。例如,有什么方法可以默认选择第二个单选按钮。 或者,我会知道QMBT-00000 参考,那么有没有办法通过搜索该文本来选择单选按钮?

我试过了:

radiobutton2 = driver.find_element_by_xpath('//*[contains(text(), "QMBT-0029104") and @id="getAttachmentValue"]');
radiobutton2.click()

但是,这给了我一个错误:

无法定位元素

【问题讨论】:

    标签: python css selenium selenium-webdriver element


    【解决方案1】:

    第一个问题很好。

    如果你需要选择的单选按钮总是第二个选项,你可以通过索引来选择它(下面是C#,但Python应该类似):

     // get all elements where id = "getAttachmentValue" 
    var radioButtons = driver.FindElements(By.Id("getAttachmentValue"));
    
     // click second element 
    radioButtons[1].Click();
    

    编辑(Python):

    from selenium.webdriver.common.by import By
    
    radioButtons = driver.find_elements(By.ID, 'getAttachmentValue')
    
    radioButtons[1].click()
    

    我将您的 HTML 添加到一个简单的 HTML 文件中,并且能够使用上面的示例(使用 C#)选择第二个单选按钮:

    【讨论】:

    • 这里的问题是每个单选按钮都有相同的 HTML 标签 ID。这在技术上不是正确的 HTML,但许多 Web 框架默认使用单选按钮来执行此操作。烦人,但无论如何我们都必须解决这个问题。
    • 这已经奏效了,太棒了!但是我遇到了另一个问题。偶尔会有超过 2 个单选按钮,但我需要选择的总是底部的那个。例如,如果有 5 个单选按钮,我希望它点击第 5 个。不管有多少按钮,有没有办法总是选择列表中的最后一个按钮?
    • 我不再经常使用 Python,但从我读到的内容中,您可以使用 -1 获得 an 的最后一个元素,即用 radioButtons[1].click() 替换 radioButtons[-1 ].click()
    【解决方案2】:

    您可以使用数组索引表示法通过 xpath 选择第二个单选按钮。不要以value 属性为目标,而是使用name 属性。 name 属性的值似乎是一致的:

    xpath = "(//input[@type = 'radio' and @name = 'getAttachmentValue'])[2]"
    radio_button = driver.find_element_by_xpath(xpath)
    radio_button.click()
    

    您可能不应该在定位器中使用id 属性。整个网页的 Id 属性值必须是唯一的,但每个单选按钮的 Id 属性值都是重复的。这是无效的 HTML。最好避免在那些属性的值违反 HTML 规范的定位器中使用属性。你永远不会完全知道浏览器会如何处理它。

    对于您的用例,name 属性效果最好。具有重复的 name 属性值是完全有效的 HTML,并且 name 属性的值对于每个页面视图似乎都是稳定的。

    【讨论】:

      【解决方案3】:

      如果你只有两个相同的 id,答案很简单。试试这个。

      driver.find_element_by_css_selector("#getAttachmentValue:nth-of-type(2)")
      

      如果有更多 - 解决方案可能会更复杂。 怎么会有 ID?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多