【问题标题】:Find Text in Selenium Java在 Selenium Java 中查找文本
【发布时间】:2018-11-17 11:44:23
【问题描述】:
<div data-ng-if="!newRequestReceived" class="ng-scope">
                <!-- ngIf: !authRequestCount --><p data-ng-if="!authRequestCount" class="ng-scope" style="" xpath="1">No more candidates awaiting Authorization</p><!-- end ngIf: !authRequestCount -->
                <!-- ngIf: authRequestCount -->
            </div>

我需要帮助,

如果找到像“No more candidates awaiting Authorization”这样的字符串

循环将如何工作???

driver.findElement(By.xpath("//p[@class='ng-scope'][contains('No more candidates awaiting Authorization']"));

这是正确的

【问题讨论】:

  • '循环将如何工作???;你在这行是什么意思?
  • 没有循环......你的意思是使用driver.findElements(不是复数的s)?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

要提取字符串没有更多候选人等待授权,您不需要这样的循环。由于该元素是 Angular 元素,您需要诱导 WebDriverWait 并且您可以使用以下任一解决方案:

  • CssSelector:

    String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ng-scope[data-ng-if*='newRequestReceived'] p.ng-scope[data-ng-if*='authRequestCount']"))).getAttribute("innerHTML");
    
  • XPath:

    String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");
    

【讨论】:

  • 非常感谢,我想在已经打开的 Firefox 选项卡中进行测试。帮帮我
  • 实际上我需要找到等待授权的候选人,在 Mettl Online Portal 中,实际上我们(100 人)尝试鼠标单击返回并授权按钮,我希望在 selenium 自动化中轻松处理此操作,以便我正在尝试....在我的程序中:打开 Firefox -> 登录页面 -> 插入用户和密码并登录 -> 单击所有测试 - > 单击授权按钮,在我的问题是当找到文本“没有更多候选人等待授权”之后点击授权按钮,GoTo All Test -> 点击Authorize,如果找到候选人,它将停止,一旦审核通过,然后goTo同样的东西Alltest ->授权
  • @YaserArafath 我已经按照我理解的最佳方式回答了这个问题。如果您的要求发生了变化,请随时提出新问题。 Stackoverflow 贡献者将很乐意为您提供帮助。
  • @YaserArafath StackOverflow 的贡献者将无法访问 Mettl 在线门户。因此,当您提出问题时,您必须提供相关的 HTML 供我们参考,以帮助我们为您构建答案。
  • while (true){ WebElement myString = null; String tt = "没有更多的候选人";尝试 { String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived') ]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML"); } 捕捉(NoSuchElementException ex){ 休息; } if (myString == tt) { System.out.println(myString); driver.findElement(By.xpath("所有测试")).click(); driver.findElement(By.xpath("Authorize")).click(); } }
猜你喜欢
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
  • 2022-07-11
  • 2015-06-23
  • 2022-01-26
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多