我已经使用Selenium WebDriver编写测试,我在平板电脑视图中打开菜单栏时出现问题。
部分的HTML代码:
<div class="hidden-xs col-xs-4 col-sm-6 col-md-5 col-lg-6 search_base">
<div class="assortment user_data_inner hidden-sm hidden-xs">
<a href="/c/N00"><input class="btn-black" type="button" value="Shop"></a>
</div>
<div id="js-mobile-nav-icon" class="sticky-hambruger-container assortment user_data_inner hidden-md hidden-lg">
<a class="sticky-burger-a">
<div class="sticky-hamburger-clickable">
<div class="sticky-header-hamburger"></div>
<div class="sticky-header-hamburger"></div>
<div class="sticky-header-hamburger"></div>
</div>
</a>
</div>
</div>
在移动设备上,我使用find元素By.cssSelector,它工作正常,菜单打开,名称为Products和test pass。
public void openHam() throws Exception {
((JavascriptExecutor)driver).executeScript("document.title = 'Opening hamburger...'");
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#js-mobile-nav-icon")));
driver.findElement(By.cssSelector("#js-mobile-nav-icon")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("body > div.mobilenav-cover.js-mobnav-close > div > div.mobilenav-cat-header.font-bold"))).getText().equals("Products");
}
但在平板电脑上,相同的代码无效。
我收到错误消息
org.openqa.selenium.TimeoutException:预期的条件失败: 等待元素可点击:By.cssSelector:
js-mobile-nav-icon(试用了10秒钟,间隔为500 MILLISECONDS)
如果我删除wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#js-mobile-nav-icon")));
我收到错误
org.openqa.selenium.ElementNotVisibleException
我还尝试将代码修改为
public void openHam() throws Exception {
((JavascriptExecutor)driver).executeScript("document.title = 'Opening hamburger...'");
WebElement elem = driver.findElement(By.cssSelector("#js-mobile-nav-icon"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(js, elem);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#js-mobile-nav-icon")));
actions.moveToElement(driver.findElement(By.cssSelector("#js-mobile-nav-icon")));
actions.perform();
driver.findElement(By.cssSelector("#js-mobile-nav-icon")).click();
}
但我还没有找到解决方案。那么有什么建议吗?