gpt4 book ai didi

java - Selenium 如何在 md-sidenav 中单击

转载 作者:行者123 更新时间:2023-11-30 10:29:59 28 4
gpt4 key购买 nike

我有一个带有 md-sidenav 的网页,我正在尝试使用 selenium 进行测试。我在页面上所做的一切都有效,除非我尝试单击 md-sidenav 中的元素。当使用 driver.findElement(By.name("elementName") 或通过 id 访问 md-sidenav 内的 web 元素时,我看到该元素不为空。此外,当访问 an 时,我可以访问它的所有子 -元素。但是当我尝试单击它们时,它给了我一个 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> &nbsp;
<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();
}
}
}

任何帮助将不胜感激!

最佳答案

尝试添加一些等待所述元素可见:

WebDriverWait wait = new WebDriverWait (driver,20);
wait.until(ExpectedConditions.visibilityOf(select));

select.click();

如果您仍然无法单击该元素,请尝试使用以下答案之一中提到的 Actions 类

关于java - Selenium 如何在 md-sidenav 中单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795477/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com