简体   繁体   English

Select 单选按钮使用 selenium 值发生变化

[英]Select radio button using selenium with value that changes

I need to select the radio button below with value="QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW"我需要 select 下面的单选按钮value="QMBT-0029104.xlsx;SYPFJPC2MQHC-5-688478#QHBTW"

HTML: 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>  

So far I have been able to click on it using the code below:到目前为止,我已经能够使用以下代码单击它:

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

However, the value changes every time which means that it's not something that I can use when running the code.但是,该值每次都会更改,这意味着它不是我在运行代码时可以使用的东西。 Is there any way to select the second radio button by default for example.例如,有什么方法可以让 select 默认为第二个单选按钮。 Alternatively, I will know the QMBT-00000 reference, so is there a way to select the radio button by searching for that text?或者,我会知道QMBT-00000参考,那么有没有办法通过搜索该文本来 select 单选按钮?

I have tried:我努力了:

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

However, that gives me an error:但是,这给了我一个错误:

Unable to locate element无法定位元素

Great first question.很好的第一个问题。

If the radio button you need to select will always be the second option, you can select it by the index (below is in C#, but should be similar for Python):如果您需要的单选按钮 select 将始终是第二个选项,您可以通过索引 select 它(下面是 C#,但对于 Python 应该类似)

 // get all elements where id = "getAttachmentValue" 
var radioButtons = driver.FindElements(By.Id("getAttachmentValue"));

 // click second element 
radioButtons[1].Click();

Edit (Python):编辑(Python):

from selenium.webdriver.common.by import By

radioButtons = driver.find_elements(By.ID, 'getAttachmentValue')

radioButtons[1].click()

I added your HTML to a simple HTML file and was able to select the second radio button using the above example (using C#):我将您的 HTML 添加到一个简单的 HTML 文件中,并且能够使用上面的示例(使用 C#)对第二个单选按钮 select:

在此处输入图像描述

You can select the second radio button by xpath using array index notation.您可以使用数组索引表示法 xpath select 第二个单选按钮。 Instead of targeting the value attribute, use the name attribute instead.不要以value属性为目标,而是使用name属性。 The value of the name attribute seems to be consistent: name 属性的值似乎是一致的:

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

You probably should not use the id attribute in your locator.您可能不应该在定位器中使用id属性。 The Id attribute value must be unique for the entire web page, but the Id attribute value is repeated for each radio button.整个 web 页面的 Id 属性值必须是唯一的,但每个单选按钮的 Id 属性值都是重复的。 This is invalid HTML.这是无效的 HTML。 It is best to avoid using attributes in locators where the values of those attributes violate the HTML spec.最好避免在这些属性的值违反 HTML 规范的定位器中使用属性。 You never fully know how the browser is going to treat it.您永远不会完全知道浏览器将如何处理它。

For your use case, the name attribute would work best.对于您的用例, name属性效果最好。 It is perfectly valid HTML to have repeated name attribute values, and the value of the name attribute appears to be stable for each page view.具有重复的名称属性值是完全有效的 HTML,并且名称属性的值对于每个页面视图似乎都是稳定的。

If you have only two same ids, the answer is simple.如果你只有两个相同的 id,答案很简单。 Try this.尝试这个。

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

If there are more - the solution may be more complicated.如果有更多 - 解决方案可能会更复杂。 How may IDs are there? ID 怎么可能存在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM