猿问

如何从硒的下拉列表中选择选项

如何在 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-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>

我尝试通过 Actions 类、Select 类没有效果。当我单击列表时可见,但我无法找到该元素。硒看不到它。


小怪兽爱吃肉
浏览 143回答 3
3回答

天涯尽头无女友

Work order number从dropdown“诱导”WebDriverWait和“跟随”xpath 选项中进行选择elementToBeClickable。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 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')]"));&nbsp; &nbsp;&nbsp;List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));public void selectOption(String option){&nbsp; &nbsp; wait.until(ExpectedConditions.elementToBeClickable(dropdown));&nbsp; &nbsp; dropdown.click();&nbsp; &nbsp; wait.until(ExpectedConditions.visibilityOfAllElements(options));&nbsp; &nbsp; for(WebElement element : options){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(element.getText().equals(option))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element.click();&nbsp; &nbsp; }}

Smart猫小萌

使用下面的代码。&nbsp; &nbsp; WebDriverWait wait = new WebDriverWait(driver, 30);&nbsp; &nbsp; WebElement dropdown = driver.findElement(By.xpath(".//ul[starts-with(@class,'el-scrollbar__view')]"));&nbsp; &nbsp; List<WebElement> options = driver.findElements(By.xpath(".//li[starts-with(@class,'el-select-dropdown__item')]"));&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testCase1() {&nbsp; &nbsp; &nbsp; &nbsp; wait.until(ExpectedConditions.elementToBeClickable(dropdown));&nbsp; &nbsp; &nbsp; &nbsp; dropdown.click();&nbsp; &nbsp; &nbsp; &nbsp; wait.until(ExpectedConditions.visibilityOfAllElements(options));&nbsp; &nbsp; &nbsp; &nbsp; for (WebElement element : options) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element.click();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答