【问题标题】:Selenium Webdriver cannot choose a value in a dropdown listSelenium Webdriver 无法在下拉列表中选择值
【发布时间】:2015-05-19 08:12:07
【问题描述】:

我正在尝试使用 Java 实现 Selenium Webdriver。 基本上,我有一个空白字段的网站。用户单击该字段后,将出现一个包含 5 个选项的下拉列表,用户应选择一个选项。

代码如下所示

<!-- language: lang-html -->
<div class="default-form w-border scheduleAddFrom" style="display: block;">
<div>
<div class="section frameless nopadding nomargin" data-form-element="SectionHeading" style="min-width: 100%;">
<div class="section-body frameless nopadding nomargin">
<div class="default-form">
<div class="form-row required-message hidden" style="min-height: 25px;">
<div class="form-row print-avoid-page-break" data-form-element="FieldEdit" style="min-height: 25px;">
<label for="">Department</label>
<input id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display" class="ui-autocomplete-display validate widget" type="text" autocomplete="off">
<span class="ui-autocomplete-display-icon"></span>
<div class="subhidden">
<select id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId" class="validate widget " data-default-value="" tabindex="5000" data-display-id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display">
   <option value=""></option>
   <option value="OPT1">Option 1</option>
   <option value="OPT2">Option 2</option>
   <option value="OPT3">Option 3</option>
   <option value="OPT4">Option 4</option>
   <option value="OPT5">Option 5</option>
</select>

我尝试使用此 Java 代码选择选项 2

WebDriverWait wait = new WebDriverWait(driver, 100);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".form-row.print-avoid-page-break>label")));

//Start to find the element. The ID is dynamically randomly generated by the system each time the page loads except the last part TaskID, thus looking for the string TaskID   
Select dropdown = new Select (driver.findElement(By.xpath(".//*[contains(@id,'TaskId')]"))); 

dropdown.selectByValue("OPT2");

Selenium 返回错误

org.openqa.selenium.ElementNotVisibleException:元素不可见:元素当前不可见,可能无法操作

我感觉这是&lt;div class="subhidden"&gt;引起的,但我不太确定。 任何建议都受到高度赞赏。谢谢。

【问题讨论】:

  • 您能否将 xpath 更改为 -//select[contains(@id,'TaskId')] 上面的输入标签在 id 中也有 TaskId。你可能是对的,选择在 div 下可能会被隐藏。

标签: java select selenium xpath webdriver


【解决方案1】:

下面的测试代码块看起来不错。 查看html 这是一个公平的假设,即选择器不会唯一地返回意图元素。我相信您想要包含id TaskIdSelect 标签。只需简单地使用//select[contains(@id,'TaskId')] 进行标签相关搜索,因为&lt;input id="Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display" 也具有具有相同文本TaskId 的ID,并且我建议您在将其插入测试之前测试xpath,至少从我的经验来看。

WebDriverWait wait = new WebDriverWait(driver, 100);
By selectElementSelector = By.xpath("//select[contains(@id,'TaskId')]");
WebElement selectElement = wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector));
Select dropdown = new Select (selectElement);
dropdown.selectByValue("OPT2");

编辑

另一个可能的选项可以使用正确选择器查找该元素。同样,您必须测试选择器以确保它是唯一的并且是您想要的。

WebDriverWait wait = new WebDriverWait(driver, 100);
String optionToSelect = "OPT1";
//Selector is the trick here
By selectElementSelector = By.cssSelector("select[id*='TaskId']>option[value='" + optionToSelect + "']");

wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector)).click();

【讨论】:

  • 非常感谢您的回复。刚刚测试过,不幸的是它仍然返回相同的错误消息“org.openqa.selenium.ElementNotVisibleException:元素不可见:元素当前不可见,可能无法操作”。我什至把 Thread.sleep(5000);在每一行中确保在 selenium 选择它之前生成列表。但这无济于事。它在步骤 dropdown.selectByValue("OPT2"); 中失败;
  • 您能否通过单击select 元素来打开上面代码块之前的选项下拉菜单。我不确定那里是否会发生任何ajax
  • selectElement.click();添加 click() 和 Selenium 说“org.openqa.selenium.ElementNotVisibleException:元素不可见”。是的,它是基于 ajax 的网页。我想知道为什么它不可见。
  • 你如何测试xpath?你能测试//select[contains(@id,'TaskId')]是否返回多个元素吗?
  • 非常感谢。不幸的是,它仍然不起作用。它在步骤 wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector)).click(); 中失败带有错误消息“org.openqa.selenium.ElementNotVisibleException:元素不可见:元素当前不可见并且可能无法操作”
【解决方案2】:

要单击不可见元素,请尝试使用操作。 首先单击 - 打开下拉列表。 Next - 将光标移动到元素的坐标,然后点击坐标。

Actions build = new Actions(driver);
By YourSelectItem = By.XPath("xPath to find");
By YourOptionToClick= By.XPath("xPath to find");
IWebElement el  =  driver.FindElement(YourSelectItem));
IWebElement el  =  driver.FindElement(YourOptionToClick));
build.MoveToElement(el).Click().MoveToElement(YourOptionToClick).Click().Build().Perform();

对不起,C#,我不能写 java 模拟:)

【讨论】:

    【解决方案3】:

    我在这上面的 2c 价值 ...

    我把上面的 HTML 放入一个文件中,保存为 html 并在 FF 中打开。然后我使用 Selenium IDE 记录下拉选择。一旦转换为 Java,我得到了这个:

     new Select(driver.findElement(By.id("Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId"))).selectByVisibleText("Option 2");
    

    我对此进行了测试,效果很好。

    【讨论】:

      【解决方案4】:

      找到根本原因。我的感觉是对的。是由&lt;div class="subhidden"&gt;引起的,因为是隐藏元素,Selenium 无法触及。 (不确定是否有任何解决方法来处理隐藏元素)。

      网页有 2 层。第 1 层是可见的下拉框,最终用户可以在其中选择值。

      第 1 层的 HTML 代码如下所示:

          <!-- language: lang-html -->
      <input id="Schedule-00-Row831efe50-8f53-4c0f-b9b0-9b622221c62cE-TaskId-Display" class="ui-autocomplete-display validate widget" type="text" autocomplete="off">
          <div class="ui-calendar">
          <ul class="ui-autocomplete-list hidden" style="opacity: 0; visibility: hidden; top: 347px; left: 213px;">
          <li class="visible " data-value=""></li>
          <li class="visible" data-value="OPT1">Option 1</li>
          <li class="visible" data-value="OPT2">Option 2</li>
          <li class="visible" data-value="OPT3">Option 3</li>
          <li class="visible" data-value="OPT4">Option 4</li>
          <li class="visible" data-value="OPT5">Option 5</li>
      

      后面还有一层,是这样的

      <!-- language: lang-html -->
      <span class="ui-autocomplete-display-icon"></span>
      <div class="subhidden">
      <select id="Schedule-00-Row831efe50-8f53-4c0f-b9b0-9b622221c62cE-TaskId" class="validate widget " data-default-value="" tabindex="5000" data-display-id="Schedule-00-Row831efe50-8f53-4c0f-b9b0-9b622221c62cE-TaskId-Display">
      <option value=""></option>
      <option value="OPT1">Option 1</option>
      <option value="OPT2">Option 2</option>
      <option value="OPT3">Option 3</option>
      <option value="OPT4">Option 4</option>
      <option value="OPT5">Option 5</option>
      </select>
      

      因此用户将从第 1 层的下拉框中选择值,然后将选择的值传输到第 2 层的下拉框进行进一步处理。

      我试图对 Selenium 进行编码以从第 2 层(隐藏)的下拉框中选择值,但没有意识到还有另一个可以单击的层。

      知道了根本原因后,Selenium 的代码就这么简单了

      <!-- language: lang-java -->
      WebElement Box = driver.findElement(By.xpath(".//*[contains(@id,'TaskId-Display')]"));
      Box.click();
      
      WebElement List = driver.findElement(By.xpath("html/body/ul[1]/li[2]"));
      DivisionList.click();
      

      总之,我是在强迫 Selenium 从隐藏的下拉列表中选择一个值。

      @Saifur,非常感谢您的讨论。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        相关资源
        最近更新 更多