Using Watir-webdriver : Getting the text of h1 tag
Asked Answered
F

1

6

I am working on Watir webdriver and I want to get text of the h1 tag of the page

<body class="not-front not-logged-in page-node node-type-first-drive one-sidebar sidebar-right col-2 bp col-2 lightbox-processed" data-twttr-rendered="true">
<div class="container">
<div id="header">
<div class=""></div>
<div class="col-center">
<h1>New Page header</h1>

The code that I am writing is

@title2 = @browser.h1.attribute_value "text"
    puts @title2

BUT the output that I am getting is Nil instead of "New Page header"

Formic answered 29/2, 2012 at 9:38 Comment(0)
I
6

Have you tried

puts @browser.h1.text

or

puts @browser.div(:class => 'col-center').h1.text
Ingate answered 29/2, 2012 at 11:29 Comment(4)
Thanks kinofrost, it works that way but just wanted to know why attribute_value is not able to get the text.Formic
@Formic Because attribute_value pulls attribute values from inside the tags in the DOM. So where you have, say <h1 style="position: relative">Titleness</h1> you can use browser.h1.attribute_value(:style) to get "POSITION: relative", but text is a method of watir-webdriver that pulls the text from between the tags.Ingate
Great, got that !! Thanks KinofrostFormic
Indeed. Helped immensely by a question that had all the needed info, the HTML in question, the code he was trying, what he expected and what he got instead. If all questions were done like that from the start things would go so much faster and smoother around here.Wacky

© 2022 - 2024 — McMap. All rights reserved.