如何使用 Java 通过 Selenium WebDriver 访问不可见的无序列表元素
How to access invisible Unordered List element with Selenium WebDriver using Java
这是 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>
列表中的项目数量因用户权限而异,因此列表中的每个元素编号不固定。
列表是常闭的,只有点击才能打开。
所以:
Select dropdown = new Select(driver.findElement(By.id("personalBar")));
dropdown.selectByVisibleText("Get einstein™ Activities");
出现错误:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "ul"
尝试此操作时:
driver.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
它失败了:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
以下内容应该有所帮助 -
driver.findElement(By.id("personal")).findElement(By.id("dropDownArrow")).click();
driver.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
首先,您不能使用 Select
抽象来处理 ul -> li
- 它旨在与 select -> option
一起使用。
你的第二种方法失败了,因为你首先需要点击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();
这是一般的想法 - 首先打开列表,然后单击一个项目 - 我的代码和细节可能不准确,因为我无法重现问题和测试提案。
如果存在三个级联 div
,则定位代码如下:
首先通过 id BodyContainer
找到主要 div
然后通过 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();
这是 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>
列表中的项目数量因用户权限而异,因此列表中的每个元素编号不固定。
列表是常闭的,只有点击才能打开。 所以:
Select dropdown = new Select(driver.findElement(By.id("personalBar")));
dropdown.selectByVisibleText("Get einstein™ Activities");
出现错误:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "ul"
尝试此操作时:
driver.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
它失败了:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
以下内容应该有所帮助 -
driver.findElement(By.id("personal")).findElement(By.id("dropDownArrow")).click();
driver.findElement(By.xpath("//li[contains(text(),'Manage your einstein™ Account')][@class='translate']")).click();
首先,您不能使用 Select
抽象来处理 ul -> li
- 它旨在与 select -> option
一起使用。
你的第二种方法失败了,因为你首先需要点击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();
这是一般的想法 - 首先打开列表,然后单击一个项目 - 我的代码和细节可能不准确,因为我无法重现问题和测试提案。
如果存在三个级联 div
,则定位代码如下:
首先通过 id BodyContainer
找到主要 div
然后通过 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();