简体   繁体   English

如何使用Java使用Selenium WebDriver访问不可见的无序列表元素

[英]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.

相关问题 如何使用Java从Selenium WebDriver中的不可见下拉元素中选择选项 - How to select option from invisible drop-down element in Selenium WebDriver using Java Selenium WebDriver,Java将显示为isDisplay的不可见元素 - Invisible element is detected as isDisplayed by Selenium WebDriver, Java 如何使用Selenium Webdriver滚动下拉列表并选择不可见/隐藏元素? - How to scroll the dropdown and select the invisible/hidden element using selenium webdriver? 如何使用 selenium webdriver 悬停并单击不可见的元素? - How to Hover over and click on an invisible element using selenium webdriver? 如果对象在Selenium Webdriver Java中不可见,如何使用xpath验证对象 - how to verify object using xpath if object is invisible in selenium webdriver java 如何使用 Selenium JAVA 点击无序列表 - How to click on unordered list using Selenium JAVA 使用Webdriver Selenium单击下一个列表元素-Java - Clicking on the next list element using Webdriver selenium - Java 如何使用 Selenium WebDriver 使用 Java 确定元素的颜色? - How to determine the color of an element using Java using Selenium WebDriver? 如何使用 Java 在 Selenium WebDriver 中检查元素是否可点击 - How to check if element is clickable in Selenium WebDriver using Java 如何在 selenium webdriver java 中使用 css 选择器定位元素? - How to locate element using css selector in selenium webdriver java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM