Selenium - 点击锚标签,该标签位于ul标签内,属性样式=" display:none;

时间:2018-04-19 21:38:56

标签: java selenium xpath css-selectors display



<div id="menu1Div" style="float: right;">
	<ul id="menu1" class="ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" role="menu" tabindex="0" aria-activedescendant="ui-id-2">
		<li class="ui-menu-item" role="presentation">
			<a href="#" aria-haspopup="true" id="ui-id-2" class="ui-corner-all" tabindex="-1" role="menuitem">
				<span class="ui-menu-icon ui-icon ui-icon-carat-1-e"></span>
				<span class="ui-icon ui-icon-grip-dotted-vertical"></span>  Actions
			</a>
			<ul class="menuUL ui-menu ui-widget ui-widget-content ui-corner-all" style="display: none; top: 4px; left: 101.8px;" role="menu" aria-expanded="false" aria-hidden="true">
				<li class="menuLI ui-menu-item" id="MenuKey_14" role="presentation">
					<a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(14)" id="ui-id-3" tabindex="-1" role="menuitem">
						<span style="margin-right: 10px;" class="fa fa-desktop"></span>Account Details
					</a>
				</li>
				<li class="menuLI ui-menu-item" id="MenuKey_1" role="presentation">
					<a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(1)" id="ui-id-6" tabindex="-1" role="menuitem">
						<span style="margin-right: 10px;" class="fa fa-search"></span>Change Password
					</a>
				</li>
				<li class="menuLI ui-menu-item" id="MenuKey_2" role="presentation">
					<a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(2)" id="ui-id-7" tabindex="-1" role="menuitem">
						<span style="margin-right: 10px;" class="fa fa-files-o"></span>My Works
					</a>
				</li>
				
				<li class="menuLI ui-menu-item" id="MenuKey_3" role="presentation">
					<a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(3)" id="ui-id-9" tabindex="-1" role="menuitem">
						<span style="margin-right: 10px;" class="fa fa-user"></span>My Page
					</a>
				</li>
				<li class="menuLI ui-menu-item" id="MenuKey_4" role="presentation">
					<a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(4)" id="ui-id-10" tabindex="-1" role="menuitem">
						<span style="margin-right: 10px;" class="fa fa-briefcase"></span>My Resources
					</a>
				</li>
				
				
			</ul>
		</li>
	</ul>
</div>
&#13;
&#13;
&#13;

有一个登录页面,完成登录后会加载下面提到的html。因此,为了获得可点击元素,我尝试按如下方式进行Thread睡眠:

Thread.sleep(3000L);

即使在此之后,它也说没有找到元素。现在我想编写一个自动化的selenium代码来点击&#34;更改密码&#34;这是嵌套李。 我尝试了不同的选择器,但结果却是负面的。

Selenium代码:

List<WebElement> elems = driver.findElements(By.cssSelector("ul.menuUL>li.menuLI.ui-menu-item>a"));
elems.get(0).click();//for the 1st element
elems.get(1).click();//for the 2nd element
elems.get(2).click();

HTML:

<div id="menu1Div" style="float: right;">
    <ul id="menu1" class="ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" role="menu" tabindex="0" aria-activedescendant="ui-id-2">
        <li class="ui-menu-item" role="presentation">
            <a href="#" aria-haspopup="true" id="ui-id-2" class="ui-corner-all" tabindex="-1" role="menuitem">
                <span class="ui-menu-icon ui-icon ui-icon-carat-1-e"></span>
                <span class="ui-icon ui-icon-grip-dotted-vertical"></span>  Actions
            </a>
            <ul class="menuUL ui-menu ui-widget ui-widget-content ui-corner-all" style="display: none; top: 4px; left: 101.8px;" role="menu" aria-expanded="false" aria-hidden="true">
                <li class="menuLI ui-menu-item" id="MenuKey_14" role="presentation">
                    <a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(14)" id="ui-id-3" tabindex="-1" role="menuitem">
                        <span style="margin-right: 10px;" class="fa fa-desktop"></span>Account Details
                    </a>
                </li>
                <li class="menuLI ui-menu-item" id="MenuKey_1" role="presentation">
                    <a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(1)" id="ui-id-6" tabindex="-1" role="menuitem">
                        <span style="margin-right: 10px;" class="fa fa-search"></span>Change Password
                    </a>
                </li>
                <li class="menuLI ui-menu-item" id="MenuKey_2" role="presentation">
                    <a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(2)" id="ui-id-7" tabindex="-1" role="menuitem">
                        <span style="margin-right: 10px;" class="fa fa-files-o"></span>My Works
                    </a>
                </li>

                <li class="menuLI ui-menu-item" id="MenuKey_3" role="presentation">
                    <a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(3)" id="ui-id-9" tabindex="-1" role="menuitem">
                        <span style="margin-right: 10px;" class="fa fa-user"></span>My Page
                    </a>
                </li>
                <li class="menuLI ui-menu-item" id="MenuKey_4" role="presentation">
                    <a class="menuAnchor ui-corner-all" href="#" style="padding-left: 0.7em;" onclick="actionSelected(4)" id="ui-id-10" tabindex="-1" role="menuitem">
                        <span style="margin-right: 10px;" class="fa fa-briefcase"></span>My Resources
                    </a>
                </li>


            </ul>
        </li>
    </ul>
</div>

4 个答案:

答案 0 :(得分:0)

List<WebElement> elems = driver.findElements(By.cssSelector("ul.menuUL>li.menuLI.ui-menu-item>a"));

应该解决你的问题。

同样在您的HTML代码中有style="display: none;,因为其中的帐户详细信息更改密码我的工作我的页面我的资源不可见。即使您尝试Thread.sleep(3000L);,该元素仍然不可见,您将无法点击所需的元素。

答案 1 :(得分:0)

如果查看以下行,请在HTML代码中

<ul class="menuUL ui-menu ui-widget ui-widget-content ui-corner-all" style="display: none; top: 4px; left: 101.8px;" role="menu" aria-expanded="false" aria-hidden="true">

您将看到 display:none; 属性。这将隐藏元素及其所有子元素。因此,selenium可以找到元素,但无法执行操作。

解决方案1 ​​

你必须要求开发人员显示元素。 (删除display:none;表示元素)。但问题是为什么它首先隐藏起来?

解决方案2

为该元素触发javascript命令而不是selenium命令。即使元素在页面上不可见,JavaScript命令也会起作用。

List elems = driver.findElements(By.cssSelector("ul.menuUL>li.menuLI.ui-menu-item>a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elems.get(0)); // select the element index from elemennt list as you need

答案 2 :(得分:0)

您可以使用以下代码执行操作: -

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//ul[@class='menuUL ui-menu ui-widget ui-widget-content ui-corner-all']"));

js.executeScript("arguments[0].setAttribute('style', 'display: fixed;')",element);

List<WebElement> elems = driver.findElements(By.cssSelector("ul.menuUL>li.menuLI.ui-menu-item>a"));

  elems.get(0).click();

答案 3 :(得分:0)

@Prathibha和@ debugger89指出了正确的方向,即包含<ul>标记的<li>标记的属性为 style =“display:none; 。所以要点击带有文字的元素更改密码,您可以使用以下代码块:

import java.util.List;
import org.openqa.selenium.JavascriptExecutor;  
// lines of code
WebElement ulContainer = driver.findElement(By.cssSelector("ul.menuUL.ui-menu.ui-widget.ui-widget-content.ui-corner-all[role='menu']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", ulContainer);
List<WebElement> elems = driver.findElements(By.cssSelector("ul.menuUL li.menuLI.ui-menu-item[role='presentation'] > a"));
for(WebElement ele:elems)
    if(ele.getAttribute("innerHTML").contains("Change Password"))
    {
        ele.click();
        break;
    }