无法使用python在selenium中通过xpath单击单选按钮元素 [英] not able to click radio button element by xpath in selenium using python

查看:43
本文介绍了无法使用python在selenium中通过xpath单击单选按钮元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的 HTML

<div id="slectrole" class="collapse in" role="tabpanel" aria-labelledby="selectrole">
<div class="panel-body">
<div class="dropdown">
<input class="search-control jsSayt jsRolesFreeText" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Eg: Delivery, BPO, Driver'" placeholder="Eg: Delivery, BPO, Driver" value="" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown" type="text">
<ul class="jsSaytList jsRolesFilter">
<li id="jsFilter_subRole_1" class="checkbox-inline jsFilterSubRole jsRoleValue_1" data-value="Accountant">
<input id="Accountant" class="radio-custom jsFilterRadio jsRole" value="Accountant" name="Role" data-roleid="1" type="radio">
<label class="radio-custom-label" for="Accountant">Accountant</label>
<div class="panel-body"><div class="下拉菜单"><input class="search-control jsSayt jsRolesFreeText" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Eg: Delivery, BPO, Driver'" placeholder="Eg: Delivery, BPO, Driver"value="" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown" type="text"><ul class="jsSaytList jsRolesFilter"><li id="jsFilter_subRole_1" class="checkbox-inline jsFilterSubRole jsRoleValue_1" data-value="会计"><input id="Accountant" class="radio-custom jsFilterRadio jsRole" value="Accountant" name="Role" data-roleid="1" type="radio"><label class="radio-custom-label" for="Accountant">Accountant</label>

Below is the code I am using to click the radio button:

以下是我用来单击单选按钮的代码:

wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='slectrole']/descendant::li[@data-value='Accountant']/label[@for='Accountant']"))) driver.find_element_by_xpath("//div[@id='slectrole']/descendant::li[@data-value='Accountant']/label[@for='Accountant']").click()

The code runs ok but it does not select the radio button.

代码运行正常,但没有选择单选按钮.

解决方案

推荐答案

好的,所以我可以理解您的沮丧,我尝试了您的代码,但无法 .click()(选择)通过 xpath 定位时的元素.请参阅下面的打印屏幕:如您所见,当通过位于 CSS 的元素发出 .click() 时,它只是点击了 radio-button.>

问题 1:您是否以一种或另一种方式绑定到 xpath 定位器策略?

Question No.1: Are you bound to the xpath locator strategy in one way or another?

如果不是,则只需使用规范的 CSS 选择器:'input[id="Accountant"]'.否则,您必须弄清楚您正在测试的网站出了什么问题,或者切换到另一个 WebElement 定位器策略.(例如:IDClassCSSLinkText 等)

If NOT, then just use a regulat CSS selector: 'input[id="Accountant"]'. Else, you have to figure out what is wrong with the website you are testing, or switch to another WebElement locator strategy. (e.g.: ID, Class, CSS, LinkText, etc.)

如果您选择使用 CSS 定位器策略,那么您的代码将如下所示:

If you would opt to go with the CSS locator-strategy, then your code would look like this:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']")))
driver.find_element_by_css("input[id='Accountant']").click()

或者,您可以尝试单击附加到 radio-button 标签,这在我的控制台中的工作方式相同:

Alternatively, you can try to click on the <label> tag attached to the radio-button, which in my console works the same way:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']")))
driver.find_element_by_css("label[for='Accountant']").click()

说明:在现实场景中,您可以通过实际的单选按钮或其标签来选择radio-button.这就是您的解决方案奏效的原因.

Explanation: In a real-life scenario, you can select the radio-button both via the actual radio-button, or via its label. That's why your solution worked.

问题 2: 为什么要使用这么长的 xpath 选择器?

Question No.2: Why are you using such a long xpath selector?

为了获得最佳选择器,您应该始终使用最短的标签/属性组合,以唯一标识您的目标元素.否则,您将容易受到网站更改、不稳定的测试用例等的影响.

In order to have a optimal selector, you should ALWAYS go with the shortest, combination of tags/attributes that will UNIQUELY identify your target element. Else you will be susceptible to website changes, flaky test cases, etc.

这篇关于无法使用python在selenium中通过xpath单击单选按钮元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆