我如何从带有div标签的下拉菜单中选择元素[重复]

问题描述 投票:0回答:2
我正在尝试从具有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>

ni到目前为止的代码是:

页面对象:

@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”。我不确定该如何处理,因为我以前只使用过selects。

假设我想为代理代码选择“ 9TH AVE”。我将如何处理?

感谢您的帮助!谢谢。

java html maven selenium automation
2个回答
0
投票
使用此xpath并获取所有选项标题(findelement)。

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

一旦有了网络元素列表,您就可以遍历每个条目并比较innerHTML

yourListOFElements.get(i).getAttribute("innerHTML")

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

希望你有主意。


0
投票
我看到您的下拉列表包含搜索字段<input class="selectator_input" placeholder="Search here..." autocomplete="false">

最好的方法是

    id="selectator_LocationListDD"选择主div
  1. 选择主区域内的搜索字段。
  2. 在搜索字段中输入选项名称的唯一部分。
  3. 然后单击主分区内唯一显示的<li>。>
  4. 以这种方式避免使用索引,索引可能会经常更改,并且会在选择中使用文本,这很可能取决于您插入的测试数据,因此您可以完全控制它。
© www.soinside.com 2019 - 2024. All rights reserved.