Java 如何从selenium中的下拉列表中选择选项
如何在每个选项上单击SeleniumJava 如何从selenium中的下拉列表中选择选项,java,selenium,Java,Selenium,如何在每个选项上单击Selenium <div class="el-select-dropdown__wrap el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;" xpath="1"> <ul class="el-scrollbar__view el-select-dropdown__list"> <!----> <li class="el-sele
<div class="el-select-dropdown__wrap el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;" xpath="1">
<ul class="el-scrollbar__view el-select-dropdown__list">
<!---->
<li class="el-select-dropdown__item selected hover" style="">
<span>Part number</span>
</li>
<li class="el-select-dropdown__item">
<span>Work order number</span>
</li>
</ul>
</div>
我尝试了按类操作,选择类没有效果。
虽然我点击列表是可见的,但我无法找到元素。Selenium没有看到它。从
下拉列表中选择工作单编号
,诱导WebDriverWait
和元素可禁用
以及以下xpath选项
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='el-select-dropdown__wrap el-scrollbar__wrap']/ul[@class='el-scrollbar__view el-select-dropdown__list']//li[./span[text()='Work order number']]")));
element.click()
或
从下拉列表中选择工作单编号
,导入WebDriverWait
和elementToBeClickable
以及以下xpath选项
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='el-select-dropdown__wrap el-scrollbar__wrap']/ul[@class='el-scrollbar__view el-select-dropdown__list']//li[./span[text()='Work order number']]")));
element.click()
或
使用以下代码:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));
List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));
public void selectOption(String option){
wait.until(ExpectedConditions.elementToBeClickable(dropdown));
dropdown.click();
wait.until(ExpectedConditions.visibilityOfAllElements(options));
for(WebElement element : options){
if(element.getText().equals(option))
element.click();
}
}
WebDriverWait wait=newwebdriverwait(驱动程序,30);
WebElement dropdown=driver.findElement(By.xpath(“.//ul[以(@class,'el-scrollbar\uu view'))开头]);
List options=driver.findElements(By.xpath(“.//li[以(@class,'el-select-dropdown\uu item'))开头]);
public void selectOption(字符串选项){
wait.until(ExpectedConditions.elementtobelickable(下拉菜单));
下拉列表。单击();
等待.直到(预期条件.所有元素的可视性(选项));
for(WebElement:options){
if(element.getText().equals(选项))
元素。单击();
}
}
希望它对您有用。使用以下代码:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));
List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));
public void selectOption(String option){
wait.until(ExpectedConditions.elementToBeClickable(dropdown));
dropdown.click();
wait.until(ExpectedConditions.visibilityOfAllElements(options));
for(WebElement element : options){
if(element.getText().equals(option))
element.click();
}
}
WebDriverWait wait=newwebdriverwait(驱动程序,30);
WebElement dropdown=driver.findElement(By.xpath(“.//ul[以(@class,'el-scrollbar\uu view'))开头]);
List options=driver.findElements(By.xpath(“.//li[以(@class,'el-select-dropdown\uu item'))开头]);
public void selectOption(字符串选项){
wait.until(ExpectedConditions.elementtobelickable(下拉菜单));
下拉列表。单击();
等待.直到(预期条件.所有元素的可视性(选项));
for(WebElement:options){
if(element.getText().equals(选项))
元素。单击();
}
}
希望它对您有用。使用以下代码
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));
List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));
@Test
public void testCase1() {
wait.until(ExpectedConditions.elementToBeClickable(dropdown));
dropdown.click();
wait.until(ExpectedConditions.visibilityOfAllElements(options));
for (WebElement element : options) {
element.click();
}
}
WebDriverWait wait=newwebdriverwait(驱动程序,30);
WebElement dropdown=driver.findElement(By.xpath(“.//ul[以(@class,'el-scrollbar\uu view'))开头]);
List options=driver.findElements(By.xpath(“.//li[以(@class,'el-select-dropdown\uu item'))开头]);
@试验
公共无效测试用例1(){
wait.until(ExpectedConditions.elementtobelickable(下拉菜单));
下拉列表。单击();
等待.直到(预期条件.所有元素的可视性(选项));
for(WebElement:options){
元素。单击();
}
}
使用以下代码
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));
List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));
@Test
public void testCase1() {
wait.until(ExpectedConditions.elementToBeClickable(dropdown));
dropdown.click();
wait.until(ExpectedConditions.visibilityOfAllElements(options));
for (WebElement element : options) {
element.click();
}
}
WebDriverWait wait=newwebdriverwait(驱动程序,30);
WebElement dropdown=driver.findElement(By.xpath(“.//ul[以(@class,'el-scrollbar\uu view'))开头]);
List options=driver.findElements(By.xpath(“.//li[以(@class,'el-select-dropdown\uu item'))开头]);
@试验
公共无效测试用例1(){
wait.until(ExpectedConditions.elementtobelickable(下拉菜单));
下拉列表。单击();
等待.直到(预期条件.所有元素的可视性(选项));
for(WebElement:options){
元素。单击();
}
}
这是结构。请从代码块中删除注释,并对代码及其回答问题的方式添加一些解释。使用显式等待等待元素可单击和可见。调用方法selectOption(“零件号”)。它将单击下拉列表,传递的字符串将被比较,如果匹配,它将从列表中选择该选项。请从代码块中删除注释,并为代码及其回答问题的方式添加一些解释。使用显式等待等待元素可单击且可见。调用方法selectOption(“零件号”)。它将单击下拉列表,比较传递的字符串,如果匹配,则从列表中选择该选项。