Rakesh Varma
Rakesh Varma

Reputation: 161

how to get text of <h1> tag inside div in selenium

i want text of h1 but how to get it, i dont know, i am new in selenium. Reply me. Thanks in advance. please ignore '\' at start tag of h1 and div.

      <\div class="on minimum coverage VPM2" title="This quote includes the minimum coverage levels    required by your state.">      
                <\h1>$74.80/month<\/h1>
            <\/div>

Upvotes: 2

Views: 30703

Answers (4)

Smart Coder
Smart Coder

Reputation: 1738

WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds); wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1")));

WebElement webelem = driver.findElement(By.cssSelector("h1"));

Upvotes: 1

dicle
dicle

Reputation: 1152

Here is the simple solution:

String titleElem = driver.findElement(By.xpath("//*[@class='on minimum coverage VPM2']/h1")).getAttribute("innerHTML”);

assertEquals("Working as charm", titleElem);

Upvotes: 2

david_c
david_c

Reputation: 227

The answer above is almost correct but when I had almost exactly the same question, using getText() would simply return an empty string. Regardless of whether I tried xpath or CSS to locate the element it made no difference. In fact, the location was a red herring - it was finding the element, it's just the h tag had no text to return.

Try this instead.

String text = driver.findElement(By.xpath("//*[@class="on minimum coverage VPM2']/h1")).getAttribute("innerHTML);

Upvotes: 2

Subh
Subh

Reputation: 4424

Assuming you want to get the innerHTML/text of the "h1" tag, i.e.,"$74.80/month" below java code will work:

String text = driver.findElement(By.xpath("//div[contains(@class,'on minimum coverage VPM2')]/h1")).getText();

Upvotes: 1

Related Questions