Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get the value of h1 as a string using selenium.

Here is the HTML javascript-

<script type="text/javascript">
    $(window).load(function() {    
        var $windowHeight = $(window).height() -12;
        $("#top").height($windowHeight);
        $('h1').css({
                'margin-top' : (($windowHeight) - $('h1').outerHeight())/2,
                'margin-bottom' : (($windowHeight) - $('h1').outerHeight())/2,
                'opacity' : '1.0',
                'filter' : 'alpha(opacity = 100)',
        });

        $("#container").click(function(){
            $("html, body").animate({ 
                scrollTop: $windowHeight + 50
            }, 1500);
        })

    });
    $(window).on("debouncedresize", function( event ) {
        var $windowHeight = $(window).height() -12;
        $("#top").height($windowHeight);
        $('h1').css({
                'margin-top' : (($windowHeight) - $('h1').outerHeight())/2,
                'margin-bottom' : (($windowHeight) - $('h1').outerHeight())/2
        });
    });
</script>

Here is what I've written in JAVA-

WebDriver driver = new FirefoxDriver(); 
    driver.get("view-source:http://websitename.com/");
    Thread.sleep(3000);
    JavascriptExecutor js = null;
    if (driver instanceof JavascriptExecutor) {
        js = (JavascriptExecutor)driver;
    }
    js.executeScript("h1");

Not sure if I should be using JavascriptExecutor in the first place. I'd appreciate any help. Thanks

share|improve this question
 
Hi AnV, and welcome to stackoverflow. We need a bit more information before we can answer your question - what exactly are you trying to get, when you say 'the value of h1'? Do you want the user-visible text in the h1 heading, or the full html of the h1 tag, or the css, or something else? –  vincebowdren Jul 4 '13 at 10:04
 
Hi vincebowdren, thanks. Yes, I'm trying to get the user-visible text in the h1 heading. Thanks for your reply. –  osrrev Jul 6 '13 at 11:42
add comment

2 Answers

h1 is a tag on the page. Why do you try accessing it using JavascriptExecutor? If you want to get text of the header h1 simply use such code

String text = driver.findElement(By.css("h1")).getText();

If you want to get an attribute of the tag use this code instead

String attr= driver.findElement(By.css("h1")).getAttribute(<attr-name>);
share|improve this answer
 
Hey, thanks for your reply. Yes, I want to get the text of the header h1. After running String text = driver.findElement(By.css("h1")).getText(); I'm getting this error- The method css(String) is undefined for the type By –  osrrev Jul 6 '13 at 11:40
 
Replaced css by cssSelector. The error is gone but it still doesn't work. –  osrrev Jul 6 '13 at 11:50
add comment

It works now! I was supposed to get the source of the page by using driver.getPageSource(); Not by driver.get("view-source:websitename.com/"). Stupid me. Thanks for the help! :)

share|improve this answer
 
Why are you getting the entire source of the page at all? It really is more efficient to use a call like getText() or get Attribute() to find the precise information you want. –  vincebowdren Jul 8 '13 at 8:12
 
Do I not have to use driver.get() at all? Can you maybe give me an example? –  osrrev Jul 8 '13 at 12:59
 
The answer posted by user2525437 has examples of what I mean. –  vincebowdren Jul 8 '13 at 15:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.