gpt4 book ai didi

java - 如何使用 div 标签从下拉列表中选择元素

转载 作者:行者123 更新时间:2023-12-01 18:05:57 28 4
gpt4 key购买 nike

我正在尝试从具有 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: Element 应该是“select”,但却是“div”。我不知道如何处理这个问题,因为我以前只处理过选择。

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

感谢任何帮助!谢谢。

最佳答案

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

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

一旦获得了网络元素列表,您就可以迭代每个条目并比较内部HTML

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

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

希望你明白了。

关于java - 如何使用 div 标签从下拉列表中选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60558928/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com