我尝试修改" 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) import 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"> </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类,相同的例外)。
答案 0 :(得分:0)
如错误消息所述,该元素应为&lt; select&gt;标签。但在你的情况下,它是一个span元素。很明显,识别失败了。
浏览器中呈现的元素可能看起来像选择框或组合框。但在HTML世界中,Selenium将其视为SPAN元素。
以下HTML代码包含select元素。
<select id="testid" name ="testname>
<option selected="" value="a">a</option>
<option selected="" value="b">b</option>
</select>
Selenium Code如下。
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"));
答案 1 :(得分:0)
@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();