[英]How to access invisible Unordered List element with Selenium WebDriver using Java
This is the HTML code: 这是HTML代码:
<ul id="personalBar">
<li id="upload" style="display:none;"></li>
<li id="personal">
<span class="translate"></span>
<span id="userName">qaadmin</span>
<div id="dropDownArrow"></div>
<ul>
<li id="pendingActivities" class="translate" onclick="eCommon.helper.viewAllPendingActivities()" translatekey="MANAGE_STORE">Manage Store</li>
<li class="translate" onclick="eCommon.helper.outSourceViewReports()" translatekey="UM_REPORT_MENU">Reports</li>
<li id="learnMore" class="translate" onclick="eCommon.helper.outSourceLearnMore()" translatekey="BANNER_THIRD_MSG">Learn more about einstein™</li>
<li id="DownloadEinstein" class="translate" onclick="eCommon.helper.outSourceLearnMore()" translatekey="BANNER_FOURTH_MSG">Download the einstein™ World app.</li>
<li class="translate" onclick="location.reload();" translatekey="CREATE_EINSTEIN_ACIVITIES">Create einstein™ Activities</li>
<li class="translate" onclick="eCommon.helper.outSourceGetEinsteinActivities()" translatekey="GET_EINSTEIN_ACTIVITIES">Get einstein™ Activities</li>
<li class="translate" onclick="eCommon.helper.outSourceManageYourEinsteinAccount()" translatekey="MANAGE_YOUR_EINSTEIN_ACCOUNT">Manage your einstein™ Account</li>
<li id="logOut" class="translate" onclick="am.UserManagement.doLogOut(false)" translatekey="LOGOUT">Logout</li>
</ul>
</li>
</ul>
The amount of items in the list varies according to users privileges so each element number in the list is not fixed. 列表中的项目数量根据用户权限而有所不同,因此列表中的每个元素编号都不固定。
The list is normally closed and it is opened only by clicking on it. 该列表通常是关闭的,仅通过单击即可打开。 So: 所以:
Select dropdown = new Select(driver.findElement(By.id("personalBar")));
dropdown.selectByVisibleText("Get einstein™ Activities");
Presents error: 出现错误:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "ul" org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应为“ select”但为“ ul”
When trying this: 尝试此操作时:
driver.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
It fails with: 它失败并显示:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互
Following should help - 以下应该有帮助-
driver.findElement(By.id("personal")).findElement(By.id("dropDownArrow")).click();
driver.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
First of all, you cannot approach ul -> li
with the Select
abstraction - it was designed to work with select -> option
. 首先,您不能使用Select
抽象来接近ul -> li
它被设计为与select -> option
。
You second approach fails since you first need to click on ul
to open up the list : 第二种方法失败了,因为您首先需要单击ul
打开列表 :
WebElement personalList = driver.findElement(By.cssSelector("ul#personalBar li#personal ul"));
personalList.click();
personalList.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
This is the general idea - first open up the list, then click an item - I might not be accurate in the code and details since I cannot reproduce the problem and test the proposal. 这是通常的想法-首先打开列表,然后单击一个项目-我的代码和详细信息可能不准确,因为我无法重现问题并测试提案。
If there are three cascading div
s then the code to locate works like this: 如果有三个级联div
则用于定位的代码如下所示:
First locate main div
by id BodyContainer
then locate unordered list by id connectionParent
then locate list by id divAddNewGroup_0
then locate my required button(+) using By.xpath
: 首先找到主要div
通过ID BodyContainer
然后通过ID定位无序列表connectionParent
然后找到通过ID列表divAddNewGroup_0
然后找到使用我需要按钮(+) By.xpath
:
driver.findElement(By.id("BodyContainer"))
.findElement(By.id("connectionParent"))
.findElement(By.id("divAddNewGroup_0"))
.findElement(By.xpath("//*[@id='divAddNewGroup_0']/span[1]/i"))
.click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.