【问题标题】:How to get value from <h1> tag using class in Selenium WebDriver, Java如何使用 Selenium WebDriver、Java 中的类从 <h1> 标记中获取值
【发布时间】:2017-11-27 01:12:46
【问题描述】:

我有html代码:

<div class="formCaptionContainer fill-width" data-dyn-bind="sizing: { width: $dyn.layout.Size.available }">
<h1 class="formCaption" data-dyn-bind="text: $data.Caption, click: $data.GoGridView">Expense report for Aditi Mehta - GS1-000282, testing2</h1>
<h2 class="formCaption-context" data-dyn-bind="text: $data.ParentTitleFields">Aditi Mehta : GS1-000282</h2>
</div>

我想从 /h1 标签获取 Aditi Mehta - GS1-000282, testing2 的价值费用报告

有人知道怎么做吗?

我试过了:

By.xpath(".//div[@class='formCaption']/h1"; 

上面显示没有找到元素

By.className("formCaption");

上面显示空白数据

By.xpath(".//*[@class='formCaption']/h1");

上面显示没有找到元素

【问题讨论】:

  • 试试这个 xpath。 "//h1[@class='formCaptain']"
  • 我厌倦了上面的,显示'没有找到这样的元素'
  • 您可以尝试添加一些等待和检查吗?
  • 是的,我也这样做了。但要么我没有找到这样的元素,要么空白数据即将到来。
  • 空白文本来意味着 xpath 是正确的。这打印了什么。 driver.findElement(By.xpath("//h1[@class='formCaptain']")).getText();

标签: java selenium webdriver


【解决方案1】:

这段代码非常适合我

       String textprint=driver.findElement(By.xpath("//h2[@class='formCaption-context']")).getText();
        System.out.println(textprint);

希望它能解决你的问题

【讨论】:

  • 不,它没有选择数据。 Xpath 正确,但数据未打印。空白数据即将到来。
  • 它为我挑选数据...使用 Thread.sleep(3000);在使用 XPath 之前。让我们看看会发生什么。我尝试了这段代码并完美地打印了值
【解决方案2】:

试试这个

String msg= driver.findElement(By.xpath(//div[contains(@class='formCaptionContainer')]/h1 ).getText();

【讨论】:

    【解决方案3】:

    你可以试试下面的 Xpath。

    String msg=driver.findElement(By.xpath("//div[@class='formCaptionContainer fill-width']/h1[@class='formCaption-context']")).getText();
    

    【讨论】:

    • 我在上面试过了,它显示文本就是这样。不选择价值。我不知道是什么问题。
    • 您是否也尝试过这些 - .getAttribute("innerText") 或 .getAttribute("textcontent") 或 .getAttribute("innerHTML")
    • 以下代码对我有用。你可以试试这个。 WebElement ele=driver.findElement(By.xpath("//div[@class='formCaptionContainer fill-width']/h1")); System.out.println(ele.getText());
    【解决方案4】:

    根据您共享的HTML,节点属性在我看来是动态的。所以我们要诱导WebDriverWait如下:

    new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//h1[@class='formCaption']"), "Aditi Mehta - GS1-"));
    System.out.println(driver.findElement(By.xpath("//h1[@class='formCaption']")).getAttribute("innerHTML"));
    

    【讨论】:

      【解决方案5】:

      您需要考虑您的locator strategy,以下为一般案例方法提供了良好的偏好。

      • 按 ID 全局唯一元素的位置
      • 按名称定位页面唯一元素
      • 通过 css 定位为捕获所有大小写

      您还应该看看PageObject pattern

      页面似乎正在使用 JS 库通过后期绑定/解析来显示内容。您的测试需要允许这样做,并等待您感兴趣的页面元素完全呈现。从开发人员那里了解元素在解决之前和之后的样子。延迟解决是确定定位器策略时的考虑因素之一。该策略应包括流畅的等待和元素出现的默认超时。

      这不是修复单个定位器的问题。

      【讨论】:

      • 你能提供我的解决方案吗,因为我尝试了很多方法,但我无法解决这个问题。
      【解决方案6】:

      感谢大家的帮助。现在工作正常。我使用下面的代码

      By headingExpenseText = By.xpath("//*[@class='formCaption'][contains(text(),'Expense report for')]");
      
      public String getTitleEx()
          {
      
              return driver.findElement(headingExpenseText).getAttribute("innerHTML");
          }
      

      【讨论】:

        【解决方案7】:

        这对我有用:

        String titleElem = driver.findElement(By.xpath("//*[@class='formCaptionContainer fill-width']/h1")).getAttribute("innerHTML");
            assertEquals("Worked", titleElem);
        

        【讨论】:

          【解决方案8】:

          您到底需要什么,网页上显示的数据或HTML代码中使用的textValue?

          如果您的定位器是正确的,那么您可以尝试getText(),而不是getAttibute("innerHTML"),如下所示,

          By.xpath("//*[@class='formCaption'][contains(text(),'Aditi Mehta')]").getAttribute("innerHTML");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-01-12
            • 2019-10-10
            • 2021-07-03
            • 2017-06-21
            • 2018-07-01
            • 2016-12-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多