Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SeleniumWebDriver和Java选择单选按钮?_Java_Internet Explorer_Selenium_Selenium Webdriver - Fatal编程技术网

如何使用SeleniumWebDriver和Java选择单选按钮?

如何使用SeleniumWebDriver和Java选择单选按钮?,java,internet-explorer,selenium,selenium-webdriver,Java,Internet Explorer,Selenium,Selenium Webdriver,你好,我正在尝试使用for选择一个单选按钮,但我似乎无法选择一个单选按钮,如果你们可以通过选择上面的任何一个单选按钮向我展示这是如何工作的,这将非常有用,并向我展示我使用这些单选按钮的方式等等 多谢各位 HTML: 我在这里使用了三个属性类型将确保它只返回单选按钮,名称将进行更多筛选以查找,确保它只找到具有匹配名称的元素,然后id以唯一标识它。请注意,您可以使用cssSelector作为[id='R001000.1']。我只是向你们展示不同的可能性 CSS第一台收音机的发射器 [name='R0

你好,我正在尝试使用for选择一个单选按钮,但我似乎无法选择一个单选按钮,如果你们可以通过选择上面的任何一个单选按钮向我展示这是如何工作的,这将非常有用,并向我展示我使用这些单选按钮的方式等等

多谢各位

HTML:


我在这里使用了三个属性<代码>类型将确保它只返回
单选按钮
名称
将进行更多筛选以查找,确保它只找到具有匹配名称的元素,然后
id
以唯一标识它。请注意,您可以使用
cssSelector
作为
[id='R001000.1']
。我只是向你们展示不同的可能性

CSS第一台收音机的发射器

[name='R001000'][id='R001000.1'][type='radio']
CSS第二台收音机选择器

[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();
}
}
List elements=driver.findElements(By.xpath(//input[@class='customCtrlLarge']);
for(WebElement:elements){
如果(!element.isSelected()){
元素。单击();
}
}

让我知道它是否工作

它的超时时间为91毫秒,不到一秒!我看不到原始代码有任何问题。我怀疑这是同步问题,请尝试添加隐式等待。看看是否工作

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

CN1=driver.findElementByCssSelector(“[name=\'R001000\”][id$='1']”);CN1.click();线程“main”org.openqa.selenium.ElementNotVisibleException:无法单击元素(警告:服务器未提供任何堆栈跟踪信息)我刚刚编辑了我的实现,你试过了吗?同样的错误,它不起作用。而且我没有使用javascript,我使用的是JavaWell,它不是
javascript
它是
java
,然后你试图找到的收音机是不可见的。但是它是手动可见的吗?上面的代码也可以使用名称而不是XPath来实现。
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();
}
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);