【问题标题】:How to locate the element as per the HTML provided?如何根据提供的 HTML 定位元素?
【发布时间】:2018-12-30 00:53:00
【问题描述】:
<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(); 

【问题讨论】:

  • 提供的 HTML 片段不包含结束标记以及链接文本。相应地更新 HTML

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


【解决方案1】:

根据您提供的用于调用 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();
    

【讨论】:

    【解决方案2】:
    findElement(By.partialLinkText("cart"))
    

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

    【讨论】:

    • 我很确定没有PrtiallinkText()这样的方法。检查你的答案并用正确的方法更新它
    • 能否请您提供完整的标签,如
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多