如何使用Selenium WebDriver选择一个组合框值,它是一个具有组合框角色的div [英] How to select a combobox value with Selenium WebDriver where it's a div with role of combobox

查看:824
本文介绍了如何使用Selenium WebDriver选择一个组合框值,它是一个具有组合框角色的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML代码有一个div标签,角色为组合框,即

 < div role =combobox> ; ...< / DIV> 

我试图从组合框中通过selenium驱动程序从java中选择一个项目。



我尝试使用选择类作为推荐在这里:



但是因为它是一个div,我得到一个错误,说:


UnexpectedTagNameException:元素应该已经被选中,但是被div


我认为这是因为div role =combobox。

有没有办法解决这个问题?

解决方案

因为没有

 选择html标记

在您的html代码中,

 Select类在这里不起作用。 

所以你可以通过两种方式来做到这一点(因为你不给你的详细html代码)

第一步:

第一步:点击该组合框。



第二步:点击组合框后,组合框选项将显示其链接文本或ID或其他定位器。



为此,请使用此代码:

  driver.findElement(By.id(search_key.combobox))。click(); // click在该组合


  driver.findElement(By.linkText(ur combo option link text)); //点击你想要的组合选项

driver.findElement(By.cssSelector( 你的组合选项的CSS路径)); / /你可以使用任何其他定位器什么是显示在你的HTML代码后点击组合框

但是在单击组合框后,如果组合选项没有在检查部分显示任何定位符,则使用以下代码:

  driver.findElement(通过。 id(search_key.combobox))。click(); //点击该组合
for(int i = 0;我< =职位; i ++){
Actions actions = new Actions(driver);
actions.sendKeys(Keys.DOWN).build()。perform(); //按下箭头键
Actions actions = new Actions(driver);
actions.sendKeys(Keys.ENTER).build()。perform(); //按回车
}

//这里position是你想要的组合框选项位置,
//为前。你想选择第三个选项,所以你的职位将是3.


My HTML code has a div tag with the role as combobox, i.e.

<div role="combobox">...</div>

I am trying to select an item from the combo box through selenium driver with java.

I tried using "Select" class as recommended here : How to select a dropdown value in Selenium WebDriver using Java

but since it is a div, I get an error saying that

"UnexpectedTagNameException: element should have been select but was div"

I think it is because of the div role="combobox".

Is there any way to resolve this issue ?

解决方案

As there is no

select html tag

in your html code,

"Select" class will not work here.

So u can do this in two ways(As u don't give ur details html code)

First process:

Step one: click on that combo box.

Step two: After click on combo box, combo box options will be shown with their link text or id or other locators.

for this, use this code:

driver.findElement(By.id("search_key.combobox")).click();//click on that combo

than

driver.findElement(By.linkText("ur combo option link text"));//click on ur desired combo option
or
driver.findElement(By.cssSelector("ur combo option's css path"));//u can use any other locator what is shown in ur html code after clicking on combo box

But after click on combo box, if combo options is not shown with any locator in inspect section, than use this code:

driver.findElement(By.id("search_key.combobox")).click();//click on that combo
for(int i = 0; i <= position; i++){
    Actions actions = new Actions(driver);
    actions.sendKeys(Keys.DOWN).build().perform();//press down arrow key
    Actions actions = new Actions(driver);
    actions.sendKeys(Keys.ENTER).build().perform();//press enter
}

//here "position" is , ur desired combo box option position,
//for ex. u want to choose 3rd option,so ur "position" will be 3.

这篇关于如何使用Selenium WebDriver选择一个组合框值,它是一个具有组合框角色的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆