如何使用 Selenium Webdriver 在 ul 中单击 li

How do I click an li inside a ul using Selenium Webdriver

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>

这是完整的 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,但我不知道如何。如果我第一次发布时没有正确提供信息,我深表歉意,所以请留下反馈,告诉我如何更好地组织我的问题,让人们更容易理解。

如果要点击第一个link,意思是

 update My details 

然后使用这个:

driver.findElement(By.cssSelector("li:nth-child(1).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();

如果要点击第二个link,意思是

 Book a Holiday 

然后使用这个:

driver.findElement(By.cssSelector("li:nth-child(2).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();

如果你想点击第三个link,那么使用这个:

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

您的 xpath 不正确,试试这个(在提供 html 代码后更新):

driver.findElement(By.xpath(".//*[@id='AddInMyQuickLinks']/div/span/ul/li[1]/h3/a")).click();

在某些浏览器中,您可以通过 right-click 检查元素,检查元素,当您在 "developer mode" 中找到正确的元素时,单击 right-click 它。

在“复制”->“复制 XPath”下。 为您提供了获得完整路径的正确方法 copy-paste.