简体   繁体   English

如何从中获取价值<h1>在 Selenium WebDriver、Java 中使用类标记

[英]How to get value from <h1> tag using class in Selenium WebDriver, Java

I have html code:我有 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>

I want to get the value Expense report for Aditi Mehta - GS1-000282, testing2 from /h1 tag我想从 /h1 标签获取 Aditi Mehta - GS1-000282, testing2 的价值费用报告

Any one know how to do it?有谁知道怎么做?

I've tried :我试过了 :

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

Above showing no element found上面显示没有找到元素

By.className("formCaption");

Above showing blank data上图显示空白数据

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

Above showing no element found上面显示没有找到元素

You need to consider your locator strategy , the following provide good preferences for general case approach.您需要考虑您的 定位器策略,以下为一般案例方法提供了良好的偏好。

  • location by ID globally unique elements通过 ID 定位全局唯一元素
  • locate by name for page unique elements按名称定位页面唯一元素
  • locate by css as catch all case通过 css 定位为捕获所有情况

You should also take a look at the PageObject pattern .您还应该看看PageObject 模式

It appears the page is using a JS library to display the contents through late binding/resolution.该页面似乎正在使用 JS 库通过后期绑定/解析来显示内容。 Your test needs allow for this and wait until the page elements you are interested in are fully rendered.您的测试需要允许这样做并等待您感兴趣的页面元素完全呈现。 Find out from the developers what the element looks like before it is resolved and afterwards.从开发人员那里了解元素在解决之前和之后的样子。 Late resolution is one considerations in determining your locator strategy.延迟解析是确定您的定位器策略时的一个考虑因素。 The strategy should include fluent waiting and default time-outs for elements to appear.该策略应包括流畅的等待和元素出现的默认超时。

This is not a problem of fixing a single locator.这不是固定单个定位器的问题。

你可以试试下面的Xpath。

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

This code work for me very well这段代码对我很有效

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

Hope It will solve your problem希望它能解决你的问题

试试这个

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

As per the HTML you have shared, the node attributes looks dynamic to me.根据您共享的HTML ,节点属性在我看来是动态的。 So we have to induce WebDriverWait as follows :所以我们必须按如下方式诱导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"));

Thanks all for your help.感谢你的帮助。 It is working fine now.它现在工作正常。 I use the below code我使用下面的代码

By headingExpenseText = By.xpath("//*[@class='formCaption'][contains(text(),'Expense report for')]");

public String getTitleEx()
    {

        return driver.findElement(headingExpenseText).getAttribute("innerHTML");
    }

This worked for me:这对我有用:

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

What do you need exactly, Data displayed on web page or textValue used in HTML code?您究竟需要什么,网页上显示的数据或 HTML 代码中使用的 textValue?

If your locator is correct , then instead of getText() , you can try getAttibute("innerHTML") like below,如果您的定位器是正确的,那么您可以尝试使用getAttibute("innerHTML")代替getText() ,如下所示,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM