如何使用Selenium Webdriver:从带有标签<aria-activedescendant>的动态组合框中选择文本?

时间:2018-04-09 23:44:47

标签: selenium

这是html:

<select select2="" id="id_event_operator" value.bind="model.operator" 
style="width:100%;display:block" class="au-target select2-hidden-accessible" 
au-target-id="134" data-select2-id="id_event_operator" tabindex="-1" aria- 
hidden="true"> <option model.bind="op.id" class="au-target" au-target-id="135" 
data-select2-id="2">--- None ---</option><option model.bind="op.id" class="au- 
target" au-target-id="135" data-select2- 
id="11">liveeventuplynktest01@test.com</option><option model.bind="op.id" 
class="au-target" au-target-id="135" data-select2- 
id="12">uplynkcmsseleniumtestaccount@test.com</option><!--anchor--> </select>


<span class="select2 select2-container select2-container--default select2- 
container--below select2-container--open" dir="ltr" data-select2-id="1" 
style="width: 100%;"><span class="selection">

<span class="select2-selection 
select2-selection--single" role="combobox" aria-haspopup="true" aria- 
expanded="true" tabindex="0" aria-labelledby="select2-id_event_operator- 
container" aria-owns="select2-id_event_operator-results" aria- 
activedescendant="select2-id_event_operator-result-q5d6- 
liveeventuplynktest01@test.com">

<span class="select2-selection__rendered" 
id="select2-id_event_operator-container" role="textbox" aria-readonly="true" 
title="--- None ---">--- None ---</span>

<span class="select2-selection__arrow" 
role="presentation"><b role="presentation"></b></span></span></span>

<span class="dropdown-wrapper" aria-hidden="true"></span></span>

当我将鼠标悬停在组合框中的元素上时,“aria-activedescendant”的值不断变化。

如何获取组合框中的值并将其设置为文本框元素。单击“class =”select2-selection select2-selection - 单个“元素”不是在我通过它执行时设置文本框值代码,但如果我从UI手动执行它,它就可以工作。

This is how the web page looks like

这是我的代码:

public class LiveEventsPage extends PageObject {

@FindBy(xpath = "/html/body/router- 
view/div[4]/div/div[2]/div/form/ul/li[1]/span/span[1]/span/span[2]")
private WebElement eventOperatorDropDownButton;

public void setEventOperator(String dropDownElem){
    waitForElemToBeClickable(eventOperatorDropDownButton).click();
    driver.findElement(By.xpath("//span[contains(@aria- 
    activedescendant,'liveeventuplynktest01@test.com')]")).click();
}
}

1 个答案:

答案 0 :(得分:0)

使用此Xpath进行定位:

 if(driver.findElement(By.xpath("//span[@aria-labelledby='select2-id_event_operator-container']")).isDisplayed())
 {
 String getData = driver.findElement(By.xpath("//span[@aria-labelledby='select2-id_event_operator-container']")).getAttribute("aria-activedescendant");
 }