gpt4 book ai didi

java - 按钮 ng 下拉-如何使用 webdriver 和 java 选择项目

转载 作者:行者123 更新时间:2023-12-02 12:00:22 25 4
gpt4 key购买 nike

我有一个带有下拉项目的按钮,如果我单击该按钮,它会打开列表并选择该项目下面是html

<button id="btn-append-to-body" class="btn btn-primary mobile-quick-button dropdown-toggle" type="button" uib-dropdown-toggle="" aria-haspopup="true" aria-expanded="false">
<div class="clearfix">
<span class="pull-left text-left ng-binding" tabindex="0"> Select one </span>
<span class="pull-right text-right ng-binding">
</div>
</button>
<ul class="uib-dropdown-menu dropdown-menu" role="menu" aria-labelledby="btn-append-to-body">
<!-- ngRepeat: option in dropOptions -->
<li id="quickOption" class="ng-scope" role="presentation" name="quickOption" ng-repeat="option in dropOptions" ng-click="selectOption(option)" required="" tabindex="0" style="">
<a href="">
<div class="clearfix">
<span class="pull-left ng-binding">frame number</span>
</div>
</a>
</li>
<!-- end ngRepeat: option in dropOptions -->
<li id="quickOption" class="ng-scope" role="presentation" name="quickOption" ng-repeat="option in dropOptions" ng-click="selectOption(option)" required="" tabindex="0">
<a href="">
<div class="clearfix">
<span class="pull-left ng-binding">serial number</span>
</div>
</a>
</li>
</ul>

我想从此列表中选择任何一项,

public void lookupSearch (String item){
driver.findElement(By.xpath("//*[@id='btn-append-to-body']")).click();
//then i choose/click the parameter item (i.e frame number or serial number)
}

将项目作为参数传递

请指导我如何选择该项目

最佳答案

要单击带有下拉项的按钮并从此列表中选择任何一项,您可以使用以下代码块:

public void lookupSearch (String item)
{
driver.findElement(By.xpath("//button[@id='btn-append-to-body']/div/span[contains(.,'Select one')]")).click();
WebDriverWait wait4elements = new WebDriverWait(driver, 10);
List<WebElement> myElements = wait4elements.until(ExpectedConditions.numberOfElementsToBe(By.xpath("//ul[@class='uib-dropdown-menu dropdown-menu']/li/a/div/span"), 2));
for(WebElement elem:myElements)
if(elem.getAttribute("innerHTML").contains(item))
{
elem.click();
break;
}
System.out.println("Element with text as "+ item +" is selected");
}

关于java - 按钮 ng 下拉-如何使用 webdriver 和 java 选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47303792/

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