简体   繁体   English

通过Selenium Java选择单选按钮

[英]selecting radio button through selenium Java

this is HTML, i just want to test it through selenium web driver(java). 这是HTML,我只想通过Selenium Web Driver(java)对其进行测试。 there is an error 有一个错误

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible 线程“主” org.openqa.selenium.ElementNotVisibleException中的异常:元素不可见

May be below HTML will help you to understand my question 可能低于HTML会帮助您理解我的问题

<html>
<head></head>
<body>
<ul class="list-unstyled">
<li>
<label class="chkbox">
<input type="radio" required="required" data-optioncode="displayDefaultForm"            data-statusid="22" data-module="19" name="status">
<span class="lbl"></span>  
</label>
<button class="btn module-status-style btn-xs mb5" style="background-color: #ffffff; border-color: #e1e1e1; color:#666666;">Unqualified</button>
 </li>
 <li>
 <label class="chkbox">
 <input type="radio" required="required" data-optioncode="displayDefaultForm" data-statusid="23" data-module="19" name="status">
 <span class="lbl"></span>
 </label>
 <button class="btn module-status-style btn-xs mb5" style="background-color: #e4e7ea; border-color: #cccccc; color:#636e7b;">Attempted to Contact</button>
</li>
<li>
<label class="chkbox">
<input type="radio" required="required" checked="" data-optioncode="displayDefaultForm" data-statusid="24" data-module="19" name="status">
<span class="lbl"></span>
</label>
<button class="btn module-status-style btn-xs mb5" style="background-color: #5bc0de; border-color: #46b8da; color:#ffffff;">Contacted</button>
</li>
<li>
<label class="chkbox">
<input type="radio" required="required" data-optioncode="displayConvertedForm" data-statusid="25" data-module="19" name="status">
<span class="lbl"></span>
</label>
<button class="btn module-status-style btn-xs mb5" style="background-color: #1caf9a; border-color: #17a08c; color:#ffffff;">Converted</button>
</li>
</ul>
</body>
</html>

please help me to check radio button through java selenuim 请帮助我通过java selenuim检查单选按钮

The exception means that the element is not visible. 异常表示该元素不可见。 Selenium cannot act on elements that are not visible, since it is intended to emulate user behavior. 硒不能作用于不可见的元素,因为它旨在模拟用户行为。 Check your CSS styles and ensure that the element you are trying to click is visible. 检查CSS样式,并确保要单击的元素可见。 If you need to click a hidden element, use JavascriptExecutor to execute a JavaScript click directly. 如果需要单击隐藏的元素,请使用JavascriptExecutor直接执行JavaScript单击。

According to your provided HTML you should try as below to select a radio :- 根据您提供的HTML,您应尝试按以下方式选择收音机:-

String textToFindRadio = "Unqualified"
//you can provide also "Attempted to Contact" or "Contacted" or "Converted" to select that specific radio.

driver.findElement(By.xpath("//input[@type = 'radio' and (following::button[contains(text(), '" + textToFindRadio + "')])]")).click();

Hope it will work...:) 希望它能工作... :)

As Saurabh said there is no element present in your code with id frm-modulestatuses . 正如Saurabh所说,您的代码中没有ID为frm-modulestatuses的元素。 I have checked your code and for clicking the radio button you can simply use xpath =.//ul/li[4]/label/input ,where li[4] for last button as - Xpath showing button and its working 我已经检查了您的代码,并单击了单选按钮,您可以简单地使用xpath =。// ul / li [4] / label / input,其中li [4]用于最后一个按钮为-Xpath显示按钮及其工作原理

I have the following approach for your query: Use xpath://input[@type='radio'] 我有以下查询方法:使用xpath://input[@type='radio']

Code: 码:

WebElement ele = driver.fidnElement(By.xpath("//input[@type='radio']"));
ele.click();

Try the above approach. 尝试以上方法。

I can think of two scenarios why your radio buttons are not visible. 我可以想到两种情况,为什么您的单选按钮不可见。

  1. Your radio button is not visible by the time your script clicks on it 脚本单击时,单选按钮不可见
  2. Your radio button is indeed hidden and somehow you need to make it appear, for example, click a button or expand a list. 您的单选按钮确实是隐藏的,因此您需要以某种方式使其出现,例如,单击一个按钮或展开一个列表。

The general solutions are: 通用解决方案是:

1: If your radio button is not visible by the time your script clicks on it: you will need to wait for it to appear, 1:如果在脚本单击时单选按钮不可见:您将需要等待它出现,

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[type='radio']"));

2: If your radio button is hidden, you need to somehow make it appear, I do not know how exactly your webpage will look like so you will have to figure this out on your own OR show us your webpage. 2:如果您的单选按钮处于隐藏状态,则需要以某种方式使其显示,但我不知道您的网页的外观如何,因此您将不得不自己解决这个问题或向我们展示您的网页。

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

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