【问题标题】:Selenium Web Driver - How to handle dynamic in href linkSelenium Web 驱动程序 - 如何处理 href 链接中的动态
【发布时间】:2018-09-24 14:57:00
【问题描述】:

以下是 HTML:

<a href="#report-adhoc/32|227/result">Result</a>

我这里有动态值,页面刷新时不断变化!如何处理。请指教。

Driver.findElement(By.xpath("//a[@href='#report-adhoc/**32|229**/result']")).click();

【问题讨论】:

  • 这是页面上唯一的报告吗?或者您可以看到多个具有相似 href 但具有动态部分的元素?
  • 如果您不向我们展示页面或某些 html,我们将无法帮助您。我们无法猜测页面中发生了什么。

标签: java selenium xpath webdriver webdriverwait


【解决方案1】:

虽然是动态的,

  • 如果 linkText ResultHTML DOM 中是唯一的,您可以使用 Link Text 诱导 WebDriverWait 元素可点击如下:

    • linkText

      new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Result"))).click();
      
  • 如果 linkText ResultHTML DOM不是 唯一的,您可以使用 XPath 诱导 WebDriverWait 让 元素可点击 如下:

    • xpath

      new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(@href,'report-adhoc') and contains(.,'Result')]"))).click();
      

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2022-01-28
    相关资源
    最近更新 更多