Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Selenium Webdriver java无法选择下拉选项_Java_Selenium - Fatal编程技术网

Selenium Webdriver java无法选择下拉选项

Selenium Webdriver java无法选择下拉选项,java,selenium,Java,Selenium,我已经阅读并尝试了许多类似问题的答案,但我仍然无法从下拉控件中选择一个选项。我的代码: //select a particular item in the returned list of companies from the company dropdown Select oDropdown = new Select(driver.findElement(By.name("ctl00$wnCore$ctrlBrokerSelect$ddlSelectCompany"))); oDropdo

我已经阅读并尝试了许多类似问题的答案,但我仍然无法从下拉控件中选择一个选项。我的代码:

//select a particular item in the returned list of companies from the company dropdown

Select oDropdown = new Select(driver.findElement(By.name("ctl00$wnCore$ctrlBrokerSelect$ddlSelectCompany")));

oDropdown.selectByVisibleText("broker1");
这总是导致
无法找到文本为broker1的选项
如果我尝试selecByIndex或Value,结果是相同的-找不到元素

我已经导入了
org.openqa.selenium.support.ui.Select
; 谢谢你可能提供的任何帮助

我很抱歉让事情变得更复杂。。。 我还尝试了
oDropdowm.selectByValue(“1”)
但这也会返回
“找不到值为1的选项”
,因为我在这里的HTML代码片段中没有看到“broker1”作为选项,因此错误
找不到文本为broker1的选项

您也可以尝试使用以下代码按值选择(根据提供的HTML代码片段基于代码):

2015年1月12日编辑 正如我所看到的,value属性没有改变,因此先前使用“value”的代码应该可以工作。此外,如果您想通过可见文本选择该选项,下面的代码应该适合您

try{
    //waiting 20 seconds to detect the visibility of the dropdown
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_wnCore_ctrlBrokerSelect_ddlSelectCompany")));

    Select oDropdown = new Select(element);
    oDropdown.selectByVisibleText("Anna Test Company");
}catch(Throwable e){
    System.err.println("Error while waiting for the element to be clickable: "+e.getMessage());
}

根据您的HTML代码片段,选项1没有
“Broker1”
文本

请给

selectByVisibleTest("Select Company") 

然后再试一次。

您能添加下拉列表中相关的HTML片段吗?当然,很抱歉。嗨,Micheal。你能发布你的HTML代码片段吗。我认为当您尝试选择时,您尝试选择的值不存在于DOM中。谢谢您的回答。我还尝试了o.Dropdown.selectByValue(“1”);但是它回来了“找不到值为1的选项。好的。我认为你粘贴了错误的HTML代码段。请右键单击下拉列表,使用firebug进行检查,然后获取正确的下拉代码。这里是:选择公司afdvv Albracht Insurance Agency,LLP Anna Test Company BB&;T保险服务公司。感谢您的帮助,但它仍然会带来“找不到文本为:Anna Test Company的元素。使用selectByValue时的结果相同。我想知道这是否是一个时间问题。在运行select语句之前,可能没有足够快地填充下拉列表?好的。。这可能是一种可能性。我已在Edit 12/01/15下再次编辑代码。请检查这是否适合你。是的,这是一个时间问题!我添加了Thread.wait(2000):它成功了。再次感谢你的帮助。我真的很感激。
selectByVisibleTest("Select Company")