无法根据下面的定位标记定位元素

时间:2018-12-19 06:19:25

标签: java selenium-webdriver xpath css-selectors webdriverwait

请在以下代码中建议如何找到“新业务”标签的链接。我尝试了以下xpath,但没有成功:

driver.findElement(By.linkText("NEW BUSINESS")).click
driver.findElement(By.xpath("//span[@class='hdBottomBar']/a[1]"))

HTML:

<span class="hdBottomBar">
                <a class="hdTopBar" href="javascript: void navCntl('NewBusiness','NavBar');" onmouseover="window.status='New Business';return true" onmouseout="window.status='';return true" name="newBusiness">NEW BUSINESS</a>

2 个答案:

答案 0 :(得分:0)

尝试:

//span[@class='hdBottomBar']/a[@name='newBusiness']

//span[@class='hdBottomBar']/a[text()='NEW BUSINESS']

答案 1 :(得分:0)

该元素是启用了JavaScript的元素,因此要调用click(),必须诱使 WebDriverWait 使元素可点击,并且使用以下任一解决方案:

  • linkText

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("NEW BUSINESS"))).click();
    
  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.hdBottomBar>a.hdTopBar[name='newBusiness']"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='hdBottomBar']/a[@class='hdTopBar' and @name='newBusiness'][text()='NEW BUSINESS']"))).click();