【问题标题】:Unable to locate the element as per the below anchor tag无法根据以下锚标记定位元素
【发布时间】:2019-05-19 14:40:30
【问题描述】:

请告知如何在以下代码中找到新业务标签的链接。我尝试了以下 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>

【问题讨论】:

  • “它没用”是什么意思?请发布相关的错误信息等。还为您想要的语言添加标签... Java?

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


【解决方案1】:

该元素是启用了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();
    

【讨论】:

    【解决方案2】:

    尝试:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      相关资源
      最近更新 更多