Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用div标记从下拉列表中选择元素_Java_Html_Maven_Selenium_Automation - Fatal编程技术网

Java 如何使用div标记从下拉列表中选择元素

Java 如何使用div标记从下拉列表中选择元素,java,html,maven,selenium,automation,Java,Html,Maven,Selenium,Automation,我试图从一个下拉列表中选择一个选项,该下拉列表中有一个div标记,而不是select。使用下面的代码,我可以打开相应的div,但是无法选择元素 这是HTML标记: <div id="selectator_LocationListDD" class="selectator_element single options-hidden" style="width: 100%; min-height: 35px; padding: 6px 12px; flex-grow: 0; position

我试图从一个下拉列表中选择一个选项,该下拉列表中有一个div标记,而不是select。使用下面的代码,我可以打开相应的div,但是无法选择元素

这是HTML标记:

<div id="selectator_LocationListDD" class="selectator_element single options-hidden" style="width: 
100%; min-height: 35px; padding: 6px 12px; flex-grow: 0; position: relative;">
<span class="selectator_textlength" style="position: absolute; visibility: hidden;">
</span>
<div class="selectator_selected_items">
<div class="selectator_selected_item selectator_value_">
<div class="selectator_selected_item_title">--Select--</div>
<div class="selectator_selected_item_subtitle"></div>
</div>
</div>
<input class="selectator_input" placeholder="Search here..." autocomplete="false">
<ul class="selectator_options" style="top: 73px;"><li class="selectator_option selectator_value_">
<div class="selectator_option_title">--Select--</div><div class="selectator_option_subtitle">
</div>
<div class="selectator_option_subtitle2">
</div>
<div class="selectator_option_subtitle3">
</div>
</li>
<li class="selectator_option selectator_value_CST0003970">
<div class="selectator_option_title">21ST STREET</div>
<div class="selectator_option_subtitle">1031 21st</div>
<div class="selectator_option_subtitle2">Lewiston, ID</div>
</li>
<li class="selectator_option selectator_value_CST0003214">
<div class="selectator_option_title">3RD &amp; STEVENS</div>
<div class="selectator_option_subtitle">508 W Third Ave</div>
<div class="selectator_option_subtitle2">Spokane, WA</div>
</li>
<li class="selectator_option selectator_value_CST0003956 active">
<div class="selectator_option_title">9TH AVE</div>
<div class="selectator_option_subtitle">600 S 9th Ave</div>
<div class="selectator_option_subtitle2">Walla Walla, WA</div>
</li>
<li class="selectator_option selectator_value_CST0003991">
<div class="selectator_option_title">10TH &amp; BANNOCK</div>
<div class="selectator_option_subtitle">950 W Bannock St, Ste 100</div>
<div class="selectator_option_subtitle2">Boise, ID</div>
</li>
</ul>
</div>
步骤定义:

@FindBy(id="selectator_LocationListDD")
WebElement locationDD;

public void select_locationEI(int index) throws InterruptedException {

    Thread.sleep(2000);
    locationDD.click();
    Select locationEI = new Select(locationDD);
    locationEI.selectByIndex(index+1); 
    // wait.until(ExpectedConditions.visibilityOfElementLocated
    (By.xpath("//div[@class=\"selectator_selected_item selectator_value_\"]//li["+ 
    (index+1)+"]"))).click();
    }
    @When("user added equipment for each location")
public void user_added_equipment_for_each_location() throws InterruptedException {

    atmAgreement = new AgreementsATM(driver);


    for(int ei: emptyLocation) {

        atmAgreement.click_addNewEquipment_tab();
        loaderVisibilityWait();
        loaderInvisibilityWait();


        atmAgreement.select_locationEI(ei);
        atmAgreement.enter_modelText();

        String dt = reader.getCellData("ATM", "Depositor Type", 2);
        atmAgreement.select_depositorType(dt);

        String manufacture = reader.getCellData("ATM", "Manufacturer", 2);
        atmAgreement.select_manufacturer(manufacture);

        atmAgreement.enter_terminalID();

        atmAgreement.click_addButtonEI();
        loaderVisibilityWait();
    }
    emptyLocation.clear();
}
我得到了一个org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“select”,但却是“div”。 我不知道如何处理这个问题,因为我以前只使用过select

假设我想为代理代码选择“9TH AVE”。我该怎么办


感谢您的帮助!谢谢。

使用此xpath并获取所有选项标题(findelements)

一旦有了webelements列表,就可以遍历每个条目并比较innerHTML

yourListOFElements.get(i).getAttribute(“innerHTML”)

并与所需文本进行比较。 如果匹配,可以单击该元素


希望您能理解。

我看到您的下拉列表包含搜索字段

最好的办法是

  • 使用
    id=“Selector\u LocationListDD”
  • 选择主div内的搜索字段
  • 在搜索字段中键入选项名称的唯一部分
  • 然后单击主div内唯一显示的
  • 这样可以避免使用索引,索引可能会频繁更改,并在选择中使用文本,这很可能取决于插入的测试数据,因此您可以完全控制它

    //ul//li/div[@class='selectator_option_title']
    store above element in yourListOFElements