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();
}

我试图使用xpath点击ul里面的li,我试图使用这个
driver.findElement(By.xpath("//div[@class='indent'/ul/li[1]a")).click();
但它似乎无法找到元素
<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>
&#13;
这是完整的xpath
/html/body/form[1]/div[3]/div[2]/div[1]/div/span[2]/div/div/div/span/ul/li[2]/h3/a
div有一个id,我认为我可以用它来到跨越里面的li,但我不知道怎么做。如果我第一次发布信息时没有正确提供信息,请致以道歉,所以请尽量反馈我如何更好地构建我的问题,以便让人们更轻松。
答案 0 :(得分:3)
如果您想点击第一个链接,意味着
update My details
然后使用它:
driver.findElement(By.cssSelector("li:nth-child(1).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();
如果您想点击第二个链接,意味着
Book a Holiday
然后使用它:
driver.findElement(By.cssSelector("li:nth-child(2).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();
如果您想点击第三个链接,请使用:
driver.findElement(By.cssSelector("li:nth-child(3).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();
使用以下代码可以简化:
//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
答案 1 :(得分:1)
您的xpath不正确,请尝试此操作(在提供html代码后更新):
driver.findElement(By.xpath(".//*[@id='AddInMyQuickLinks']/div/span/ul/li[1]/h3/a")).click();
答案 2 :(得分:0)
在某些浏览器中,您可以通过右键单击该元素来检查该元素,然后在“开发人员模式”下找到正确的元素时检查该元素。
在“复制”->“复制XPath”下。 通过复制粘贴为您提供了获得完整路径的正确方法。