![](/img/trans.png)
[英]Select Radio Button in a group using Selenium WebDriver with Java
[英]How to select radio button using Selenium WebDriver with Java?
您好,我正在嘗試使用for選擇一個單選按鈕,但是如果你們可以通過選擇上面的任何一個單選按鈕來向我展示其工作原理,那么我似乎無法選擇一個單選按鈕並顯示我用這些單選按鈕等操作的方式。
非常感謝你!
HTML:
<span class="radioButtonHolder">
<input type="radio" name="R001000" value="1" id="R001000.1" class="customCtrlLarge" />
</span>
<label for="R001000.1">Test 1</label>
</div>
<div class="Opt2 rbloption">
<span class="radioButtonHolder">
<input type="radio" name="R001000" value="2" id="R001000.2" class="customCtrlLarge" />
</span>
<label for="R001000.2">Test 2</label>
</div>
Java代碼:
List<WebElement> RadioGroup1 = driver.findElements(By.name("R001000"));
for (int i = 0; i < RadioGroup1.size(); i++) {
System.out.println("NUM:" + i + "/" + RadioGroup1.get(i).isSelected());
}
RadioGroup1.get(1).click();
錯誤代碼:
Started InternetExplorerDriver server (32-bit)
2.44.0.0
Listening on port 30883
NUM:0/false
NUM:1/false
NUM:2/false
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 91 milliseconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'Code-PC', ip: 'Nope', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:30883/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: cebed22f-5ae6-464b-bb1b-a18150f9e5a8
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:59)
我在這里使用三個屬性。 type
將確保僅返回radio
按鈕, name
將進行更多過濾以查找,確保其僅找到具有匹配名稱的元素,然后使用id
進行唯一標識。 請注意,您可以將cssSelector
用作[id='R001000.1']
。 我只是向您展示不同的可能性。
第一次廣播的CssSelextor
[name='R001000'][id='R001000.1'][type='radio']
第二個收音機的CssSelector
[name='R001000'][id='R001000.2'][type='radio']
執行:
By byCss = By.cssSelector("[name='R001000'][id='R001000.2'][type='radio']");
driver.findElement(byCss).click();
我不建議您在這種情況下使用for
循環。 問題是隱藏有相同名稱的單選按鈕/元素的數量可能超過預期,因此列表將為您返回所有內容。
與OP討論之后,建議使用以下代碼示例:
public void Test()
{
_driver = new FirefoxDriver();
_driver.Navigate().GoToUrl(Url);
_driver.Manage().Window.Maximize();
_driver.FindElement(By.Id("CN1")).SendKeys("7203002");
_driver.FindElement(By.Id("CN2")).SendKeys("0370");
_driver.FindElement(By.XPath("//*[@id='InputDay']/option[@value='23']")).Click();
_driver.FindElement(By.XPath("//*[@id='InputMonth']/option[@value='02']")).Click();
_driver.FindElement(By.XPath("//*[@id='InputYear']/option[@value='15']")).Click();
_driver.FindElement(By.Id("NextButton")).Click();
_driver.FindElement(By.XPath("//label[.='Lunch']//../span")).Click();
_driver.FindElement(By.XPath("//label[.='Dining room']//../span")).Click();
}
嘗試這個
List<WebElement> elements = driver.findElements(By.xpath("//input[@class='customCtrlLarge']");
for(WebElement element : elements){
if(!element.isSelected()){
element.click();
}
}
讓我知道是否有效
它的超時時間為91毫秒,不到秒! 我看不到原始代碼有任何問題。 我懷疑它存在同步問題,請嘗試添加隱式等待。 看看是否可行。
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.