【问题标题】:WebDriver Change ComboBox Selection "webkit-user-select"WebDriver 更改组合框选择“webkit-user-select”
【发布时间】:2014-05-14 17:29:55
【问题描述】:

我尝试通过各种方式修改“webkit-user-select”类型的组合框选择,但它永远不会起作用。

1)

import org.openqa.selenium.support.ui.Select;

Select select = new Select(driver.findElement(By.cssSelector("#BirthMonth")); // Exception
select.selectByValue("July"); // Select item by value

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"

2)

WebElement element = driver.findElement(By.cssSelector("#BirthMonth"));
element.click(); // Open comboBox
element.sendKeys(Keys.DOWN); // Navigate down (Exception)

org.openqa.selenium.WebDriverException: unknown error: cannot focus element

3) 导入 org.openqa.selenium.JavascriptExecutor;

((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//*[@id=\":9\"]")); // Select item October directly (Exception)

org.openqa.selenium.NoSuchElementException: no such element

HTML:

<label id="month-label" class="month">
  <span id="BirthMonth" class=" " aria-invalid="false"><div class="goog-inline-block goog-flat-menu-button jfk-select" aria-expanded="false" role="button" tabindex="0" aria-haspopup="true" title="Birthday" style="-webkit-user-select: none;"><div class="goog-inline-block goog-flat-menu-button-caption">July</div><div class="goog-inline-block goog-flat-menu-button-dropdown" aria-hidden="true">&nbsp;</div></div><div class="goog-menu goog-menu-vertical" role="listbox" aria-haspopup="true" style="-webkit-user-select: none; visibility: visible; left: 0px; top: -158.5px; display: none;"><div class="goog-menuitem" role="option" id=":0" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">January</div></div><div class="goog-menuitem" role="option" id=":1" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">February</div></div><div class="goog-menuitem" role="option" id=":2" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">March</div></div><div class="goog-menuitem" role="option" id=":3" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">April</div></div><div class="goog-menuitem" role="option" id=":4" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">May</div></div><div class="goog-menuitem" role="option" id=":5" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">June</div></div><div class="goog-menuitem" role="option" id=":6" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">July</div></div><div class="goog-menuitem" role="option" id=":7" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">August</div></div><div class="goog-menuitem" role="option" id=":8" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">September</div></div><div class="goog-menuitem" role="option" id=":9" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">October</div></div><div class="goog-menuitem" role="option" id=":a" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">November</div></div><div class="goog-menuitem" role="option" id=":b" style="-webkit-user-select: none;"><div class="goog-menuitem-content" style="-webkit-user-select: none;">December</div></div></div><input type="hidden" name="BirthMonth" id="HiddenBirthMonth" value="07"></span>
  </label>

第二个解决方案似乎部分工作,因为组合框已打开,但使用向上/向下键导航不会成功(即使使用 Actions 类也不成功,同样的例外)。

【问题讨论】:

  • 这不是普通的select。错误是这样说的。这是一组divspan 的样式,看起来像一个下拉菜单。我怀疑只是一个简单的.findElement 并得到你需要的span(通过文本,所以使用 XPath)会起作用
  • 请建议设置特定月份的代码

标签: java javascript html selenium webdriver


【解决方案1】:

@Purus 是正确的,因为您的组合框是一个跨度,您不能使用 Select 包装器,因为它只能用于 select 标记名。

我认为您尝试做的是正确的,单击组合,打开月份,然后单击适当的月份。你有没有尝试过这样的事情,

 WebDriverWait wait = new WebDriverWait(driver,30);
 WebElement element = driver.findElement(By.cssSelector("#BirthMonth"));
 element.click();
 WebElement month = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//div[text()='July']")));
      month.click(); 

【讨论】:

  • 完美运行,太棒了!我没想到那样等待和好的 xpath 建议
  • @BullyWiiPlaza 很高兴它对你有用。我记得几年前 WebDriver 处于测试版时经历过类似的选择框噩梦 :)
【解决方案2】:

如错误消息所述,该元素应属于

浏览器中呈现的元素可能看起来像一个选择框或组合框。但在 HTML 世界中,Selenium 将其视为 SPAN 元素。

下面的 HTML 代码有选择元素。

<select id="testid" name ="testname>
<option selected="" value="a">a</option>
<option selected="" value="b">b</option>
</select>

Selenium 代码如下。

Select select = new Select(driver.findElement(By.cssSelector("#testid"));
select.selectByValue("a"); // Select item by value

使用 JavaScript,请尝试以下操作。

((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.id(":9"));

【讨论】:

  • 感谢您的解释,但是处理跨度的正确方法是什么?没有 Span 类或类似的 AFAIK
  • 您必须形成自己的处理方式,因为没有特定的方法可以处理 SPAN。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
相关资源
最近更新 更多