我有一个带有md-sidenav的网页,我试图用selenium进行测试。我在页面上所做的一切都有效,除非我尝试点击md-sidenav中的元素。当使用driver.findElement(By.name(" elementName")或id访问md-sidenav中的web元素时,我看到该元素不为null。此外,访问时,我可以访问所有的子元素。但是当我尝试点击它们时,它给了我一个ElementNotVisibleException:无法点击元素
这是HTML:
<body ng-controller='MyController as my' ng-cloak>
<section layout="row" column>
<md-sidenav id="sideNavRight" class="md-sidenav-right" md-component-id="right" ng-init="my.selectTab(1)">
<div ng-controller="MyController" ng-show="my.isSelected(1)">
<md-toolbar class="md-theme-light">
<h1 class="md-toolbar-tools">Some Form</small>
<md-button id="navClose" ng-click="close()" class="closeIcon" aria-label="Close">
<md-icon md-svg-icon="assets/img/ic_close_white_24px.svg"></md-icon><sr-only>Close</sr-only>
</md-button></h1>
</md-toolbar>
<md-content>
<div layout="column" index="0">
<h3>Some Information</h3> <br />
<form name="myForm">
<div layout="row" layout-align="start" flex="100">
<md-input-container flex="">
<label>Category:</label>
<md-select name="category" required="" ng-model="menu.category" >
<md-option class="choices" value="cat1">Category1</md-option>
<md-option value="cat2">Category2</md-option>
<md-option value="cat3">Category3</md-option>
</md-select>
<div class="errors" ng-messages="myForm.category.$error">
<div ng-message="required">Required</div>
</div>
</md-input-container>
</div>
<div layout="row" class="pull-right">
<md-button class="btn" ng-click="clearValue()">Clear</md-button>
<md-button class="btn" ng-click="menu.updateCart();menu.selectTab(2);">Next</md-button>
</div>
</form>
</div>
</md-content>
</div>
</md-sidenav>
</section>
....
rest of the page
....
</body>
这是Selenium代码:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class EcsTest {
static WebDriver driver;
static Wait<WebDriver> wait;
static String prevText = "";
static String currText = "";
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver","C:\\Dvp\\Irs\\EcsTest\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
wait = new WebDriverWait(driver, 30);
driver.get("http://localhost:8080/ecs/index.html");
try {
driver.findElement(By.id("element1")).click();
wait.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver webDriver) {
currText = webDriver.findElement(By.id("field1")).getText();
return !prevText.equals(currText);
}
});
prevText = currText;
driver.findElement(By.id("element2")).click();
//driver.findElement(By.name("dispositionSelect")).sendKeys("Add to Scheme");
Select selectElement = new Select(driver.findElement(By.name("selectElement1")));
disposition.selectByVisibleText("Some form");
WebElement sideNav = driver.findElement(By.id("sideNavRight"));
WebElement closeBtn = sideNav.findElement(By.id("navClose"));
/*
This prints out:
sideNav is not null!, closeBtn is not null!, not displayed!, enabled!
*/
System.out.println(
"sideNav is " + ((sideNav == null) ? "null!" : "not null!") +
", closeBtn is " + ((closeBtn == null) ? "null!" : "not null!") +
", " + ((closeBtn.isDisplayed()) ? "displayed!" : "not displayed!") +
", " + ((closeBtn.isEnabled()) ? "enabled!" : "not enabled!"));
WebElement select = sideNav.findElement(By.name("category"));
List<WebElement> options = select.findElements(By.tagName("md-option"));
/*
This prints out:
select: category, visible: false
*/
System.out.println(
"select: " + select.getAttribute("name") + ", visible: " + select.isDisplayed());
/*
This crashes:
ElementNotVisibleException: Cannot click on element
Without this click, I can see the options fine but the option click crashes
*/
select.click();
for(WebElement option : options) {
System.out.println("option: " + option.getAttribute("value"));
if(option.getAttribute("value").equals(optionName)) {
option.click();
System.out.println("option: " + option.getAttribute("value") + " clicked!");
break;
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
finally {
driver.close();
}
}
}
任何帮助将不胜感激!
答案 0 :(得分:1)
尝试添加一些等待所述元素可见:
WebDriverWait wait = new WebDriverWait (driver,20);
wait.until(ExpectedConditions.visibilityOf(select));
select.click();
如果您仍然无法点击该元素,请尝试使用下面其中一个答案中提到的Actions class
。
答案 1 :(得分:0)
创建一个Actions类:
Actions builder = new Actions(driver);
只要你在md-sidenav中使用它就可以用于每个操作:
WebElement yourElement = sideNav.findElement(By.id("elementId"));
builder.moveToElement(yourElement).click().build().perform();
builder.moveToElement(yourElement).sendKeys("2017-05-04").build().perform();
等
编辑: 这只会导致操作不抛出和异常,但它实际上并没有执行操作。即它报告它点击了一个按钮,没有抛出任何异常,但也没有采取任何行动。 我太快快乐了。