[英]Not able to select option from drop down list in selenium webdriver with java
I'm trying to select a value from drop down list. 我正在尝试从下拉列表中选择一个值。 I have checked all posts related to it but not able to find solution. 我已经检查了所有与此相关的帖子,但找不到解决方案。
Here is my HTML code for drop down list: 这是我的HTML代码下拉列表:
<select class="select2 visible" data-val="true" data-val-number="The field ClientId must be a number." id="ClientId" name="ClientId" tabindex="-1" title="" style="display: none;">
<option value="">Client</option>
<option value="22">ABC</option>
<option value="7">ABC1</option>
<option value="18">ABC2</option>
<option value="27">ABC3</option>
<option value="26">ABC4</option>
<option value="31">ABC5</option>
<option value="12">ABC6</option>
<option value="19">ABC7</option>
<option value="72">DGX Client</option>
<option value="57">DS Sampler</option>
<option value="25">Group123</option>
</select>
the code which I written in Selenium Webdriver to fetch value: 我在Selenium Webdriver中编写的用于获取值的代码:
@FindBy(id="ClientId")
WebElement clientDropDown;
waitTime = new WebDriverWait(driver,20);
waitTime.until(ExpectedConditions.visibilityOf(clientDropDown));
Select client=new Select(clientDropDown);
client.selectByVisibleText("DGX Client");
Error: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of [[ChromeDriver: chrome on XP (6fa8cbb25476bea9b789aff19a6edf)] -> id: ClientId] (tried for 30 second(s) with 500 milliseconds interval) 错误:org.openqa.selenium.TimeoutException:预期条件失败:正在等待[[ChromeDriver:XP上的Chrome(6fa8cbb25476bea9b789aff19a6edf)]-> id:ClientId]的可见性(以500毫秒为间隔进行了30秒的尝试)
Operating under an assumption here. 在这里假设运行。
See how your select
has is hidden by the display: none
style: display: none
将隐藏您select
的内容display: none
样式:
<select ... style="display: none;">
^^^^^^^^^^^^^^^^^^^^^
I assume this is because it is actually represented on a UI differently and this select
is under-the-hood manipulated by the javascript whenever the actual dropdown representation changes. 我认为这是因为它实际上以不同的方式在UI上表示,并且只要实际的下拉表示发生更改,此select
就会由javascript进行底层操作。
If this is the case, you can either inspect what the actual dropdown looks like and use a combination of click()
commands to open the dropdown and select the desired option (note that you would not be able to use Select
class in that case as it is designed to be used for select
elements only). 在这种情况下,您可以检查实际的下拉菜单,然后使用click()
命令组合打开下拉菜单并选择所需的选项(请注意,在这种情况下,您将无法使用Select
类)它仅设计用于select
元素)。
Or, you can make the select
element visible and continue : 或者,您可以使select
元素可见并继续 :
String js = "arguments[0].style.display='block'";
((JavascriptExecutor) driver).executeScript(js, clientDropDown);
Select client = new Select(clientDropDown);
client.selectByVisibleText("DGX Client");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.