如何根据提供的HTML定位元素?

时间:2018-07-22 10:14:01

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

<a class="action showcart active" data-bind="scope: 'minicart_content'" href="http://52.14.171.179/xstpl/checkout/cart/">  

我已经尝试过此方法,但无法继续-

driver.findElement(By.linkText("cart")).click(); 

2 个答案:

答案 0 :(得分:0)

根据您提供的用于调用click()的HTML元素,您必须引入该元素才能诱导 WebDriverWait 以使所需的元素可点击,并且您可以使用以下解决方案:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.action.showcart.active[href='http://52.14.171.179/xstpl/checkout/cart/']"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='action showcart active' and @href='http://52.14.171.179/xstpl/checkout/cart/']"))).click();
    

答案 1 :(得分:-1)

findElement(By.partialLinkText("cart"))

并确保字符串"cart"区分大小写