您好我正在尝试使用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)
答案 0 :(得分:1)
我在这里使用三个属性。 type
会确保它只返回radio
按钮,name
会过滤更多以查找确保它只找到匹配名称的元素,然后id
来唯一标识它。请注意,您可以将cssSelector
用作[id='R001000.1']
。我只是向您展示了不同的可能性。
第一台电台的CssSelextor
[name='R001000'][id='R001000.1'][type='radio']
CssSelector for second radio
[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();
}
答案 1 :(得分:1)
试试这个
List<WebElement> elements = driver.findElements(By.xpath("//input[@class='customCtrlLarge']");
for(WebElement element : elements){
if(!element.isSelected()){
element.click();
}
}
让我知道它是否有效
答案 2 :(得分:0)
它在91毫秒内超时,小于秒!我没有看到原始代码的任何问题。我怀疑它是同步问题,尝试添加隐式等待。看看是否有效。
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);