java Selenium Webdriver:单击单选按钮不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41541387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Webdriver: Click on radio button not working
提问by Kawamoto Takeshi
I have a code which clicks on a radio button, at first I was using Chrome. Using the code below:
我有一个点击单选按钮的代码,起初我使用的是 Chrome。使用以下代码:
driver.findElement(By.id("radioButton1"))).click();
I got the error:
我收到错误:
"org.openqa.selenium.WebDriverException: Element is not clickable at point (411, 675). Other element would receive the click: ..."
Doing research, I changed the code to:
做研究,我将代码更改为:
actions.moveToElement(driver.findElement(By.id("radioButton1"))).click().perform();
Now, I am trying to use Internet Explorer driver. But it does not perform the click.
现在,我正在尝试使用 Internet Explorer 驱动程序。但它不执行点击。
I tried the following:
我尝试了以下方法:
driver.findElement(By.id("radioButton1")).sendKeys(Keys.ENTER);
actions.moveToElement(driver.findElement(By.id("radioButton1"))).click().perform();
((JavascriptExecutor) driver).executeScript("arguments[0].click()", driver.findElement(By.id("radioButton1")));
But none works. The first one just focuses on the button, so I added another sendKeys, but it doesn't work. The 2nd and 3rd, nothing happens.
但没有一个有效。第一个只关注按钮,所以我添加了另一个sendKeys,但它不起作用。第二个和第三个,什么也没有发生。
Edit:
编辑:
Adding HTML snippet.
添加 HTML 片段。
<input name="btn1" class="w-rdo-native" id="radioButton1" type="radio" value="value1" bh="RDOINP" isrefresh="false">
<label class="w-rdo w-rdo-dsize" bh="RDO"></label>
And when I click on the radio button, the label gets an additional property upon click.
当我点击单选按钮时,标签会在点击时获得一个额外的属性。
<label class="w-rdo w-rdo-dsize" bh="RDO" AWMouseDown="true"></label>
Additional edit:
补充编辑:
The set of buttons look like this:
这组按钮看起来像这样:
and as stated before, one button + label block has the following HTML structure:
如前所述,一个按钮 + 标签块具有以下 HTML 结构:
<tr>
<td>
<div class="w-rdo-container">
<input name="radioButtons" class="w-rdo-native" id="button1" type="radio" value="button1" bh="RDOINP" isrefresh="false">
<label class="w-rdo w-rdo-dsize" bh="RDO">
</label>
</div>
</td>
<td class="sectionHead">Option 2
</td>
</tr>
Upon clicking a button, the corresponding label gets an additional attribute:
单击按钮后,相应的标签会获得一个附加属性:
<label class="w-rdo w-rdo-dsize" bh="RDO" AWMouseDown="true"></label>
It seems AWMouseDown seems to be the trigger to 'officially' click the button.
似乎 AWMouseDown 似乎是“正式”单击按钮的触发器。
Edit :
编辑 :
Full HTML snippet of table. (Please note that this table has been cleansed so apologies for some mistake if I committed one.)
表格的完整 HTML 片段。(请注意,这张桌子已经被清理干净了,如果我犯了一个错误,请原谅。)
<table border="0" cellpadding="0" cellspacing="0" class="a-cptp-tbl">
<tbody>
<tr>
<td>
<div class="w-rdo-container">
<input checked class="w-rdo-native" id="btn1" name="radioBtn" type="radio" value="btn1"><label class="w-rdo w-rdo-dsize"></label>
</div>
</td>
<td class="sectionHead">Option 1</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div class="w-rdo-container">
<input class="w-rdo-native" id="btn2" name="radioBtn" type="radio" value="btn2"><label class="w-rdo w-rdo-dsize"></label>
</div>
</td>
<td class="sectionHead">Option 2</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div class="w-rdo-container">
<input class="w-rdo-native" id="btn3" name="radioBtn" type="radio" value="btn3"><label class="w-rdo w-rdo-dsize"></label>
</div>
</td>
<td class="sectionHead">Option 3</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div class="w-rdo-container">
<input class="w-rdo-native" id="btn4" name="radioBtn" type="radio" value="btn4"><label class="w-rdo w-rdo-dsize"></label>
</div>
</td>
<td class="sectionHead">Option 4</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div class="w-rdo-container">
<input class="w-rdo-native" id="btn5" name="radioBtn" type="radio" value="btn5"><label class="w-rdo w-rdo-dsize"></label>
</div>
</td>
<td class="sectionHead">Option 5</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div class="w-rdo-container">
<input class="w-rdo-native" id="btn6" name="radioBtn" type="radio" value="btn6"><label class="w-rdo w-rdo-dsize"></label>
</div>
</td>
<td class="sectionHead">Option 6</td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
采纳答案by user861594
Try using JavaScript like below:
尝试使用 JavaScript,如下所示:
WebElement radioBtn1 = driver.findElement(By.id("radioButton1"));
((JavascriptExecutor) driver).executeScript("arguments[0].checked = true;", radioBtn1);
If you are using QMetry Automation Framework, you should create custom radio button component like where you can override click method with such custom implementation.
如果您使用 QMetry 自动化框架,您应该创建自定义单选按钮组件,例如您可以使用此类自定义实现覆盖 click 方法。
回答by NarendraR
Use ExplicitWait
to wait for element until clickable and then have to click on that element
使用ExplicitWait
等待元素,直到点击,然后必须单击元素
WebElement element = driver.findElement(By.id("radioButton1"));
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.click();
EDITED
已编辑
If it is causing problem in IE
browser. The cause is preventing to find element in IE browser is ActiveX Controls
如果它导致IE
浏览器出现问题。阻止在 IE 浏览器中查找元素的原因是ActiveX Controls
So just you need to follow these steps -
所以你只需要按照这些步骤 -
Go to Internet options > Advanced > security and do check below mentioned checks -
after check > apply and then don't forgot to restart your PC
Now simply run your script and try to click on that element using id
现在只需运行您的脚本并尝试使用 id
driver.findElement(By.id("button1")).click();
Hope this will work. Let us know if still face the same issue.
希望这会奏效。如果仍然面临同样的问题,请告诉我们。
回答by rahul.tripathi
Not sure what is causing the problem.It works for me thought:
不确定是什么导致了问题。它对我有用,我想:
public static IWebDriver driver;
[Test]
public void TestMethod1()
{
driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("file:///C:/Users/utripra/Desktop/test.html");
driver.FindElement(By.Id("radioButton1")).Click();
回答by Guy
It seems that the radio button is combination of the <input>
and <label>
tags, i.e. the <div>
with class="w-rdo-container"
or its <td>
parent. I think so because the rapper <td>
and the <td>
where the label Option 2
is are siblings.
单选按钮似乎是<input>
和<label>
标签的组合,即<div>
withclass="w-rdo-container"
或<td>
其父级。我认为是因为说唱歌手<td>
和<td>
标签所在的地方Option 2
是兄弟姐妹。
class="w-rdo-container"
doesn't seem to be unique, so you can use xpath
to go up the html tree from id="button1"
class="w-rdo-container"
似乎不是唯一的,因此您可以使用xpath
从id="button1"
driver.findElement(By.xpath("//div[input[@id='button1']]")).click(); // to click the div
// or
driver.findElement(By.xpath("//td[div[input[@id='button1']]]")).click(); // to click the td
回答by Mahipal
Try following for clicking on Option 2radio button:
尝试以下单击选项 2单选按钮:
driver.findElement(By.xpath("//td[normalize-space(text())='Option 2']/preceding::input[1]")).click();
回答by optimistic_creeper
Write a method that will accept the position of the radio button and click on the button by using cssSelector as follows:
编写一个方法来接受单选按钮的位置,然后使用 cssSelector 点击按钮,如下所示:
driver.findElement(By.cssSelector("table.a-cptp-tbl > tbody > tr:nth-child(" + checkBoxPosition + ") > td > div > input")).click();
Full method:
完整方法:
public void selectOption(int positionOfCheckBox){
By locator = By.cssSelector("table.a-cptp-tbl > tbody > tr:nth-child(" + positionOfCheckBox + ") > td > div > input");
//wait for your element to be visible
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
//click element after it is visible/clickable
driver.findElement(locator).click();
}
回答by Anish Pillai
Can you try identifying the radio buttons using a list and then clicking on an element in the list using its index with get()?
您可以尝试使用列表识别单选按钮,然后使用 get() 使用其索引单击列表中的元素吗?
List<WebElement> radioGrp = driver.findElements(By.name("xxxxxxxx"));
radioGrp.get(0).click();
回答by Emerensiana Karmiyati
Just add Thread.sleep(5000);
above your script for radio button.
For example like this
只需Thread.sleep(5000);
在单选按钮的脚本上方添加。例如像这样
Thread.sleep(5000);
driver.findElement(By.id("uniform-id_gender2")).click();
It works for me. :)
这个对我有用。:)