繁体   English   中英

无法使用Selenium单击单选按钮

[英]Cannot click a radio button using Selenium

我已经尝试了许多xpath函数,但是总是收到错误消息,例如元素不可单击,有人建议应使用for循环,有人可以提出建议吗?

<dd class="inline">
  <!-- ngRepeat: gender in genderList track by $index --><label class="option ng-scope" data-ng-repeat="gender in genderList track by $index">
     <input name="gender" value="0" data-ng-click="genderChange($index)" type="radio">
     <span class="round"></span>
     <p class="ng-binding">All</p>
  </label><!-- end ngRepeat: gender in genderList track by $index --><label class="option ng-scope" data-ng-repeat="gender in genderList track by $index">
     <input name="gender" value="1" data-ng-click="genderChange($index)" type="radio">
     <span class="round"></span>
     <p class="ng-binding">Male</p>
  </label><!-- end ngRepeat: gender in genderList track by $index --><label class="option ng-scope" data-ng-repeat="gender in genderList track by $index">
     <input name="gender" value="2" data-ng-click="genderChange($index)" type="radio">
     <span class="round"></span>
     <p class="ng-binding">Female</p>
  </label><!-- end ngRepeat: gender in genderList track by $index -->
</dd>

您可以尝试使用以下方法:

IWebElement radioButton = driver.findElement(By.CssSelector(".yourRadioButton"));
radioButton.SendKeys(Keys.Enter);

要么

radioButton.SendKeys(Keys.Space);

有时单选按钮和其他控件对此无响应:

radioButton.Click();

您可以使用javascriptexecutor模拟单选按钮的单击

jquery = $x("//dd[@class='inline']/label[0]/input")[0].click()

或者您也可以使用javascript

==============================
Selenium Click将起作用...如果获取的元素不是可单击的异常,则可能是您的xpath错误。 确保像我上面提到的那样在xpath中选择输入标签。因为如果您选择除label或dd这样的输入以外的其他标签,则将获得element不可点击异常

你能发布你的xpath吗?

这听起来像是一个等待问题。 有时,WebDriver执行命令的速度过快,并且有可能该元素尚未完全加载到DOM中以供单击。

若要找出问题,请在单击操作之前尝试使用Thread.Sleep。 如果等待是问题,请尝试使用WebDriverWait

用这个:

JavaScriptExecutor js = driver;
js.executeScript("document.findElementById('**idOfTheRadioButton**').click");

暂无
暂无

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

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