[英]Selenium Webdriver: Click on radio button not working
我有一個點擊單選按鈕的代碼,起初我使用的是Chrome。 使用以下代碼:
driver.findElement(By.id("radioButton1"))).click();
我收到了錯誤:
"org.openqa.selenium.WebDriverException: Element is not clickable at point (411, 675). Other element would receive the click: ..."
做研究,我把代碼更改為:
actions.moveToElement(driver.findElement(By.id("radioButton1"))).click().perform();
現在,我正在嘗試使用Internet Explorer驅動程序。 但它不會執行點擊。
我嘗試了以下方法:
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")));
但都沒有效果。 第一個只關注按鈕,所以我添加了另一個sendKeys,但它不起作用。 第二和第三,沒有任何反應。
編輯:
添加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>
當我點擊單選按鈕時,標簽會在點擊后獲得額外的屬性。
<label class="w-rdo w-rdo-dsize" bh="RDO" AWMouseDown="true"></label>
附加編輯:
這組按鈕看起來像這樣:
如前所述,一個按鈕+標簽塊具有以下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>
單擊按鈕后,相應的標簽將獲得其他屬性:
<label class="w-rdo w-rdo-dsize" bh="RDO" AWMouseDown="true"></label>
似乎AWMouseDown似乎是“正式”點擊按鈕的觸發器。
編輯:
表格的完整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>
嘗試使用如下的JavaScript:
WebElement radioBtn1 = driver.findElement(By.id("radioButton1"));
((JavascriptExecutor) driver).executeScript("arguments[0].checked = true;", radioBtn1);
如果您使用的是QMetry Automation Framework,則應創建自定義單選按鈕組件,例如您可以使用此類自定義實現覆蓋click方法。
使用ExplicitWait
等待元素直到可單擊,然后必須單擊該元素
WebElement element = driver.findElement(By.id("radioButton1"));
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.click();
EDITED
如果它在IE
瀏覽器中引起問題。 阻止在IE瀏覽器中找到元素的原因是ActiveX Controls
所以只需要按照以下步驟操作 -
現在只需運行您的腳本並嘗試使用id
單擊該元素
driver.findElement(By.id("button1")).click();
希望這會奏效。 如果仍然面臨同樣的問題,請告訴我們。
您可以嘗試使用列表識別單選按鈕,然后使用帶有get()的索引單擊列表中的元素嗎?
List<WebElement> radioGrp = driver.findElements(By.name("xxxxxxxx"));
radioGrp.get(0).click();
不確定是什么導致了這個問題。它對我有用:
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();
似乎單選按鈕是<input>
和<label>
標簽的組合,即<div>
with class="w-rdo-container"
或其<td>
parent。 我是這么認為的,因為說唱歌手<td>
和標簽Option 2
所在的<td>
是兄弟姐妹。
class="w-rdo-container"
似乎不是唯一的,所以你可以使用xpath
從id="button1"
上傳html樹
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
點擊Option 2單選按鈕,嘗試以下操作:
driver.findElement(By.xpath("//td[normalize-space(text())='Option 2']/preceding::input[1]")).click();
編寫一個接受單選按鈕位置的方法,然后使用cssSelector單擊按鈕,如下所示:
driver.findElement(By.cssSelector("table.a-cptp-tbl > tbody > tr:nth-child(" + checkBoxPosition + ") > td > div > input")).click();
完整方法:
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();
}
只需添加Thread.sleep(5000);
在單選按鈕的腳本上方。 比如這樣
Thread.sleep(5000);
driver.findElement(By.id("uniform-id_gender2")).click();
這個對我有用。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.