简体   繁体   English

如何使用 selenium webdriver 从输入类型下拉列表中选择一个值?

[英]How to select a value from input type dropdown using selenium webdriver?

I am trying to select a value available in a read only drop down and I have tried so many options but still failing to select the desired option.我正在尝试在只读下拉列表中选择一个可用的值,我尝试了很多选项,但仍然无法选择所需的选项。 The drop down has two values available ValueOne and ValueTwo.下拉菜单有两个可用值 ValueOne 和 ValueTwo。 By default the ValueOne is selected and in my case I need to select ValueTwo.默认情况下选择 ValueOne,在我的情况下,我需要选择 ValueTwo。 I used firebug to get the below code when I click on the drop down and do Inspect Element with firebug The Code is :当我单击下拉菜单并使用 firebug 执行 Inspect Element 时,我使用 firebug 获取以下代码,代码为:

<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input" class="rcbInput radPreventDecorate" type="text" readonly="readonly" value="ValueOne" name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl02$EditFormControl$rcbControllerType1" autocomplete="off">
</td>

So far I have tried到目前为止我已经尝试过

1---------- 1----------

Select DropDown = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")));
        DropDown.selectByVisibleText("ValueTwo");

and I get an exception as我得到一个例外

:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"

2------------ 2------------

WebElement Dropdown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input"));
        Select clickThis = new Select (Dropdown);
        clickThis.selectByVisibleText("ValueTwo");

Get Exception:获取异常:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"

I also tried selectByIndex but still get the above exception message.我也尝试过 selectByIndex 但仍然收到上述异常消息。

3-------------- 3--------------

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo");

Nothing happens and the case is marked as Pass.没有任何反应,案例被标记为通过。 No error no exception.没有错误也没有例外。

Also I am running my webscript on firefox 38.0.5 with selenium 2.46.0 with eclipse TestNG .此外,我正在使用 eclipse TestNG使用selenium 2.46.0firefox 38.0.5上运行我的webscript I have confirmed the frame is not an iframe.我已经确认该框架不是 iframe。

Please suggest the solution.请提出解决方案。

The root problem might be, that this is not a standard select box but an javascript based solution.根本问题可能是,这不是标准的选择框,而是基于 javascript 的解决方案。 The code above just shows the 'host' html element.上面的代码只显示了'host' html 元素。 I am pretty sure that there will be a) a previous hidden element that becomes visible or b) a newly created element in your DOM that holds the values.我很确定会有 a) 一个以前的隐藏元素变得可见或 b) 在你的 DOM 中一个新创建的元素保存这些值。 You have to find those (dev tools or firebug) to interact with.您必须找到要与之交互的那些(开发工具或萤火虫)。 Some pseudo code that might appear (just to get a hint):一些可能出现的伪代码(只是为了获得提示):

<ul>
  <li id="element1">ValueOne</li>
  <li id="element2">ValueTwo</li>
</ul>

And after it appears (wait for in selenium) you just have to click the desired element.在它出现后(在 selenium 中等待),您只需单击所需的元素。

Find your xpath through firepath addon in firefox.通过firefox中的firepath插件找到你的xpath。

driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']")).click(); driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']")).click();

Select value in dropdown ->goto firpath by right click and copy xpath在下拉列表中选择值 -> 通过右键单击转到 firpath 并复制 xpath

driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']/span[3]")).click();

hope you will find your solution :-)希望你能找到你的解决方案:-)

你可以用这个:-

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo", Keys.ARROW_DOWN, Keys.ENTER)

you can use the following code.您可以使用以下代码。 Here what I have done is find the dropdown, click on it and find the option to select and send down key until we see the element to select and after click on it.在这里,我所做的是找到下拉列表,单击它并找到选择和发送键的选项,直到我们看到要选择的元素,然后单击它。

public class InputDropdownselect {
@Test
public void login() throws Exception
{
System.setProperty("webdriver.chrome.driver", "G:\\drivers\\chrome\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://yourwebsite.com");
driver.manage().window().maximize();
driver.findElement(By.id("txtuser")).sendKeys("123456");
driver.findElement(By.id("txtpassword")).sendKeys("Abc!@1");
driver.findElement(By.id("log-btn")).click();
Thread.sleep(2000);
driver.findElement(By.id("enrollment")).click();
//driver.findElement(By.xpath("//*[@id=\"enrollment\"]")).click();
driver.findElement(By.xpath("//*[@id=\"studentBasicForm\"]/div[2]/div[9]/div/div/input")).click();
Actions action= new Actions(driver);
WebElement joiningYear=driver.findElement(By.xpath("//input[@placeholder=\"Joining Year Group\"]/following::ul[1]/descendant::li/following::span[contains(text(),\"8\")]"));
do {
    action.sendKeys(Keys.ARROW_DOWN).perform();
} while (!joiningYear.isDisplayed());
joiningYear.click();
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Selenium webdriver 的“跨度类型下拉列表”中选择值 - how to select the value from “Span Type dropdown” in Selenium webdriver 当元素不是选择类型输入时,如何使用Selenium Webdriver Java从下拉列表中获取所有选项值? - how to get all option values from dropdown using selenium webdriver java when the element is not a select type input? 如何使用 Java 在 Selenium WebDriver 中选择和获取下拉值 - How to select and get dropdown value in Selenium WebDriver using Java 如何使用 Java 在 Selenium WebDriver 中选择隐藏的下拉值 - How to select a hidden dropdown value in Selenium WebDriver using Java 如何使用 Java 在 Selenium WebDriver 中选择下拉值 - How to select a dropdown value in Selenium WebDriver using Java 如何从selenium webdriver中的div列表中选择下拉值? - How to select dropdown value from div list in selenium webdriver? 如何从Selenium WebDriver的下拉框中选择值 - How to select the value from dropdown box in selenium webdriver 如何在 select 下拉值 Selenium WebDriver Java - How to select a dropdown value in Selenium WebDriver Java Selenium WebDriver:如何在范围下拉列表中选择一个值? - Selenium WebDriver: How to select a value in a span dropdown? 如何使用Selenium WebDriver和java从下拉列表中选择项目? - How to select an item from a dropdown list using Selenium WebDriver with java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM