【问题标题】:How I can select an element from a dropdown list?如何从下拉列表中选择元素?
【发布时间】:2021-06-08 09:44:23
【问题描述】:

问题:我尝试从下拉列表中选择一个元素

我有一个包含以下 HTML 代码的下拉列表:

<span class="wpcf7-form-control-wrap localitate">
<select name="localitate" class="wpcf7-form-control wpcf7-select form-control" id="cf_location_trigger" aria-invalid="false">
<option value="">Locatie</option>
<option value="Bucuresti CARAMFIL">Bucuresti CARAMFIL</option>
<option value="Bucuresti THE LIGHT">Bucuresti THE LIGHT</option>
<option value="Cluj-Napoca">Cluj-Napoca</option>
<option value="Iasi">Iasi</option></select></span>

我尝试使用以下 Java 代码选择元素但没有成功:

 location.click();
   
   WebDriverWait waits = new WebDriverWait(driver, 20);
   waits.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"cf_location_trigger\"]"))).click();
           Select dropdownLocation = new Select(driver.findElement(By.xpath("//*[@id=\"cf_location_trigger\"]")));
           dropdownLocation.selectByIndex(3);

如何选择元素,我尝试增加等待的时间,我也尝试使用Thread.sleep (),但它仍然无法识别列表中的项目或dropdownLocation.selectByText("Bucuresti CARAMFI");

【问题讨论】:

    标签: java selenium testing automated-tests


    【解决方案1】:
    dropdownLocation.selectByText("Bucuresti");
    

    我没有看到任何选项文本为Bucuresti

    尝试用那个替换:

    Bucuresti CARAMFIL
    

    添加更多内容:

    waits.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"cf_location_trigger\"]"))).click();
    

    您不需要在 Select WebElementclick,因为它会由您使用的 Select 类处理。

    您应该使用 id不要在 XPATH 中使用 ID。 除非有必要。

    类似这样的:

    Select dropdownLocation = new Select(driver.findElement(By.id("cf_location_trigger")));
    

    更新 1:

    代码顺序:

    Select dropdownLocation = new Select(driver.findElement(By.id("cf_location_trigger")));
    dropdownLocation.selectByText("Bucuresti CARAMFI");
    

    更新 2:

    WebDriverWait wait = new WebDriverWait(driver, 20);
    Actions action = new Actions(driver);
    action.moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cf_location_trigger")))).build().perform();
    Select select = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cf_location_trigger"))));
    select.selectByVisibleText("Bucuresti CARAMFI");
    

    【讨论】:

    • 程序正在运行,但无法识别项目。点击“位置”下拉菜单,但不要访问“布加勒斯特 CARAMFIL”元素
    • 我尝试了 xpath、css、id、标记名定位器和....nothing
    • ChromeDriver 启动成功。 [1623151401.636][警告]:已弃用的 chrome 选项被忽略:useAutomationExtension [1623151401.636][警告]:已弃用的 chrome 选项被忽略:useAutomationExtension [1623151402.086][警告]:此版本的 ChromeDriver 尚未使用 Chrome 版本 91 进行测试。6 月 8 日, 2021 下午 2:23:22 org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C
    • 线程“主”org.openqa.selenium.ElementClickInterceptedException 中的异常:元素点击被拦截:元素在点(1052、3403)不可点击(会话信息:chrome=91.0.4472.77)构建信息:版本:'3.141.59',修订:'e82be7d358',时间:'2018-11-14T08:17:03'
    • 这意味着驱动程序无法移动到下拉菜单。这个下拉菜单在 UI 中的什么位置?它在视口中吗?你必须滚动才能看到这个下拉菜单吗?
    【解决方案2】:

    看看这是否有效

    Select select = new Select(driver.findElement(By.xpath(".//select[@name='localitate']")));
    select.selectByVisibleText("Bucuresti CARAMFIL");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      相关资源
      最近更新 更多