简体   繁体   English

无法使用Java从Selenium Webdriver的下拉列表中选择选项

[英]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.

相关问题 无法从Selenium Webdriver的下拉列表中选择隐藏元素 - Not able to select hidden element from drop down list in Selenium Webdriver Selenium WebDriver:无法使用webdriver从下拉列表中选择元素 - Selenium WebDriver: Not able to select the element from the drop down list using webdriver selenium2:Selenium WebDriver:从“图像下拉”列表框中选择一个选项 - selenium2:Selenium WebDriver : Select one option from Image Drop Down list Box 从 Java Selenium 的下拉菜单中选择一个选项 - Select an option from drop down in Java Selenium 如何选择下拉选项,应在selenium webdriver Java代码的下拉列表中单击以下值 - how to select drop down option and should click on the following values within drop down list in selenium webdriver java code 如何使用带有Java的Selenium WebDriver从下拉列表中选择一个选项? - How to select an option from a drop-down using Selenium WebDriver with Java? 如何使用Java从Selenium WebDriver中的不可见下拉元素中选择选项 - How to select option from invisible drop-down element in Selenium WebDriver using Java 无法在Java中的Selenium Webdriver中选择下拉列表 - unable to select the drop down list in selenium webdriver in java 使用以下命令从Selenium Java Webdriver下拉菜单中选择一个值 - select a value from drop down in selenium java webdriver using 如何使用带有 Java 的 Selenium WebDriver 从下拉列表中选择项目? - How to select item from drop down list using Selenium WebDriver with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM