Selenium-Java Webdriver:我收到错误,因为当我选择值表单下拉框时,Element 应该是“选择”但是是“输入”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19515100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:56:14  来源:igfitidea点击:

Selenium-Java Webdriver: I am getting error as Element should have been "select" but was "input" when i select the value form dropdown box

javaseleniumselenium-webdriverwebdriverbrowser-automation

提问by Adnan Ghaffar

Error message "Element should have been "select" but was "input""is shown when i select the value form drop down box. I have tried with selectByValue() amd selectByIndex().

当我选择值表单下拉框时,会显示错误消息“元素应该是“选择”但被“输入”。我已经尝试过 selectByValue() 和 selectByIndex()。

HTML:

HTML:

<div class="rcbScroll rcbWidth" style="width: 100%; overflow: auto; height: 40px;">
<ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
<li class="rcbItem">TIN</li>
<li class="rcbHovered">SSN</li>
</ul>
</div>

java code is:

java代码是:

Select select=new Select(driver.findElement(By.xpath(OR.getProperty("SSN"))));
select.selectByValue("SSN");

回答by shiv

Yo can Select element by following method

你可以通过以下方法选择元素

driver.findElement(By.xpath(OR.getProperty("SSN")).click();

In Next line select desired drop down in following way :

在下一行中,按以下方式选择所需的下拉列表:

driver.findElement(By.xpath("//li[text() = 'SSN']")).click();

回答by iamsankalp89

You can apply selectto select value from drop down, there is an lielement there and you try to implement using selectthats why it thrown the exception

您可以申请select从下拉列表中选择值,那里有一个li元素,您尝试使用select这就是它抛出异常的原因来实现

回答by DebanjanB

This error message...

这个错误信息...

Element should have been "select" but was "input"

...implies that you have tried to select the value through an instance of Selectobject where as the desired element was not a part of any <select>parent tag.

...暗示您已尝试通过Select对象的实例选择值,因为所需元素不是任何<select>父标记的一部分。

As per the HTMLyou have shared to click on the optionwith text as SSNyou can use the following code block :

根据您共享的HTML,单击文本为SSN选项,您可以使用以下代码块:

driver.findElement(By.xpath("//div[@class='rcbScroll rcbWidth']")).click();
driver.findElement(By.xpath("//div[@class='rcbScroll rcbWidth']/ul//li[@class='rcbHovered' and contains(.,'SSN')]")).click();

回答by Nael Marwan

Select by will not work for you cuz you need select tag with options tags as sub tags for it. Bellow html code is the correct format you need for using Selenium Select.

Select by 对您不起作用,因为您需要选择带有选项标签的标签作为它的子标签。Bellow html 代码是使用 Selenium Select 所需的正确格式。

<select some-label-name=”display label” name=”some name” id=”some id” title=”some title” class=”class name”>
<option value=”0”>display label</option>
<option value=”1” selected=”1”>1</option>
<option value=”2”>2</option>
<option value=”3”>3</option>
</select>

To solve your problem you need to click on the dropdown list to open options then click on the option you need.

要解决您的问题,您需要单击下拉列表以打开选项,然后单击您需要的选项。