Java 如何使用 Selenium Webdriver 在 ul 中单击 li
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37054345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I click an li inside a ul using Selenium Webdriver
提问by OhAye
public void testHolidayLink() {
// login
driver.findElement(By.id("ctl00_MCPH_MainLogin_UserNameTextBox")).sendKeys("username");
driver.findElement(By.id("ctl00_MCPH_MainLogin_PasswordTextBox")).sendKeys("password");
driver.findElement(By.id("ctl00_MCPH_MainLogin_LoginButton")).click();
// click book holiday
driver.findElement(By.xpath(".//*[@id='AddInMyQuickLinks']/div/span/ul/li[1]/h3/a")).click();
}
I'm trying to click an li inside a ul using xpath, im trying to use this
我正在尝试使用 xpath 在 ul 中单击 li,我正在尝试使用它
driver.findElement(By.xpath("//div[@class='indent'/ul/li[1]a")).click();
driver.findElement(By.xpath("//div[@class='indent'/ul/li[1]a")).click();
but it can't seem to find the element
但它似乎无法找到元素
<div class="AddIn atScreens AddInViewportDESKTOP">
<div style="width:100%;" data-role="collapsible" class="AddInCollapsible ui-accordion ui-widget ui-helper-reset" id="AddInMyQuickLinks" role="tablist">
<h2 class="AddinTitleBar ui-accordion-header ui-helper-reset ui-state-default ui-accordion-header-active ui-state-active ui-corner-top ui-accordion-icons" role="tab" id="ui-accordion-AddInMyQuickLinks-header-0" aria-controls="ui-accordion-AddInMyQuickLinks-panel-0"
aria-selected="true" tabindex="0"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>My Quick Links</h2>
<div class="AddInMain ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="display: block;" id="ui-accordion-AddInMyQuickLinks-panel-0" aria-labelledby="ui-accordion-AddInMyQuickLinks-header-0"
role="tabpanel" aria-expanded="true" aria-hidden="false">
<span><ul class="NoIndent"><li class="NoBullet jms-bullet"><h3><a href="javascript:ShowSelectedAddInScreen('-1','32f3c56c-5660-488c-b348-07552ea7d299','0','false',false,'false',true, 'Update Personal Details','e1ff7f13-59fe-4bfc-842d-64a33b0dd98f','b0ab7c0f-bd9d-4f0d-9d1e-508b414ab9ec');"><img alt="Update My Details" src="..\Images\hr.png">Update My Details <span title="Keep your personal information up to date" class="AddInItemDescription">Keep your personal information up to date </span>
</a>
</h3>
</li>
<li class="NoBullet jms-bullet">
<h3><a href="javascript:ShowSelectedAddInScreen('-1','8142758e-269a-41f0-b551-433e56dd1225','0','false',false,'false',true, 'Submit a Holiday','e1ff7f13-59fe-4bfc-842d-64a33b0dd98f','4c1ea925-69e5-4950-aeae-18e8493fefd1');"><img alt="Book a Holiday" src="../Style Sheets/images/Holiday114.png">Book a Holiday <span title="Place a request for a holiday" class="AddInItemDescription">Place a request for a holiday </span></a></h3>
</li>
<li class="NoBullet jms-bullet">
<h3><a href="javascript:ShowSelectedAddInScreen('D4292FDC-7213-464C-9C96-019BD67C12DA','bebbb0b6-5328-4737-9709-f389f065db8b','0','false',false,'false',false, 'Employee Mobility','e1ff7f13-59fe-4bfc-842d-64a33b0dd98f','');"><img alt="My Mobility" src="..\Images\hr.png">My Mobility <span title="Places where I would work" class="AddInItemDescription">Places where I would work </span></a></h3>
</li>
</ul>
</span>
</div>
</div>
</div>
And this is the full xpath
这是完整的 xpath
/html/body/form[1]/div[3]/div[2]/div[1]/div/span[2]/div/div/div/span/ul/li[2]/h3/a
/html/body/form[1]/div[3]/div[2]/div[1]/div/span[2]/div/div/div/span/ul/li[2]/h3/a
The div has an id which I think i can use to get to the li inside the span but I have no idea how. Apologies if I have not provided the information correctly its my first time posting so please do leave feedback in how I can structure my questions better so as to make it easier for people.
div 有一个 id,我想我可以用它来到达跨度内的 li,但我不知道如何。抱歉,如果我第一次发帖时没有正确提供信息,请留下反馈,告诉我如何更好地组织我的问题,以便人们更轻松。
采纳答案by noor
If you want to click on the first link, meaning
如果你想点击第一个链接,意思是
update My details
then use this:
然后使用这个:
driver.findElement(By.cssSelector("li:nth-child(1).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();
If you want to click on the second link, meaning
如果你想点击第二个链接,意思是
Book a Holiday
then use this:
然后使用这个:
driver.findElement(By.cssSelector("li:nth-child(2).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();
If you want to click on the third link, then use this:
如果要单击第三个链接,请使用以下链接:
driver.findElement(By.cssSelector("li:nth-child(3).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();
This can be simplified by using the below code:
这可以通过使用以下代码来简化:
//first create a web element list which contains all elements inside a list:
List<WebElement> elems = driver.findElements(By.cssSelector("ul.NoIndent>li.NoBullet.jms-bullet> h3>a"));
//Now you can select individual elements from a list using:
elems.get(0).click();//for the 1st element
elems.get(1).click();//for the 2nd element
elems.get(2).click();//for the 3rd element
回答by ThreeDots
Your xpath is incorrect, try this (updated after html code was provided):
你的 xpath 不正确,试试这个(在提供 html 代码后更新):
driver.findElement(By.xpath(".//*[@id='AddInMyQuickLinks']/div/span/ul/li[1]/h3/a")).click();
driver.findElement(By.xpath(".//*[@id='AddInMyQuickLinks']/div/span/ul/li[1]/h3/a")).click();
回答by XDavidT
In some browsers, you can inspect the element by right-click on it, Inspect the element, when you find the correct element in "developer mode" click right-click on it.
在某些浏览器中,您可以通过右键单击元素来检查元素,检查元素,当您在“开发人员模式”中找到正确的元素时,单击右键单击它。
Under Copy->Copy XPath. Gave you the right way to get full path just copy-paste.
在复制-> 复制 XPath 下。为您提供获取完整路径的正确方法,只需复制粘贴即可。