Re: getTitle intermittently does not return title

1,223 views
Skip to first unread message

Harald Meland

unread,
Jun 28, 2012, 5:21:31 AM6/28/12
to seleniu...@googlegroups.com
On Tuesday, 26 June 2012 10:00:43 UTC+2, Harald Meland wrote:
I'm having intermittent problems using getTitle with WebDriver 2.21.0; a test that does

 1. findElement( an <a href ...> link ).click(),
 2. assert that driver.getTitle() looks as expected

sometimes fails, and sometimes passes.  In the failing cases, .getTitle() returns "".  The page it is called on is static, i.e. there isn't any JavaScript magic that updates the page's title after the page has been loaded.

I found an existing bug report for this: http://code.google.com/p/selenium/issues/detail?id=3543
... but that was just closed with "WorkingAsIntended".  I've protested this, but thought I'd also bring the issues up here, in the hope that someone might help me understand how things ought to work.

At the moment, I'm having a hard time understanding the following things:

1. WebElement.click() is documented as a method that "will attempt to block until the page has loaded" -- which does not match the suggestion given on the bug to use a WebDriverWait to ensure that the page has loaded before .getTitle will work.
2. The documentation for WebDriver.getTitle says that it should return null "if [a title] is not already set".  As mentioned above, I'm seeing the empty string rather than null in the failing cases.  Is there some intentional difference between returning "" and null?  Is the documentation or the current implementation buggy?
 
I'm suspecting my post somehow went under the radar of this group's otherwise very responsive and helpful audience.

Should suspected bugs like this be posted to the developer's group rather than here?

Or is it just me that's being too impatient? ;-)

Regards
-- 
Harald

Mark Collin

unread,
Jun 28, 2012, 6:11:33 AM6/28/12
to seleniu...@googlegroups.com

The symptoms would suggest that the page is not fully loaded when you are getting the title.  As a result I would suggest you set an explicit wait to ensure that the page has loaded before trying to get the title.

 

I would point out that is says that WebDriver will *attempt* to block until the page has loaded.  It’s very hard (read almost impossible) to be 100% sure that a page has loaded unless you put in an explicit wait that checks for something on the page that is known to only be displayed once the page has finished loading.

 

Assuming this is a valid problem:

 

·         Do you have a minimal test case that can regularly reproduce this behaviour?

·         Does this happen in all browsers (It’s possible the browser you are using is creating a <title> element and then populating it afterwards and WebDriver is getting the title text before the browser has finished rendering the <title> element in the DOM correctly)?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/xILlo0mJ20kJ.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Harald Meland

unread,
Jun 28, 2012, 6:44:54 AM6/28/12
to seleniu...@googlegroups.com
On Thursday, 28 June 2012 12:11:33 UTC+2, Mark Collin wrote:

The symptoms would suggest that the page is not fully loaded when you are getting the title.  As a result I would suggest you set an explicit wait to ensure that the page has loaded before trying to get the title.

That is exactly what was suggested on the bug report I mentioned... but I'd really like to know a bit more about when I can/can't trust WebDriver's heuristic for determining whether a page has loaded.  If I just go with your suggestion without having obtained this knowledge first, I suspect that I would make my tests significantly less readable by adding lots and lots of redundant explicit waits.
 

I would point out that is says that WebDriver will *attempt* to block until the page has loaded.  It’s very hard (read almost impossible) to be 100% sure that a page has loaded unless you put in an explicit wait that checks for something on the page that is known to only be displayed once the page has finished loading.

I read the documentation as saying, in practice: WebDriver will wait until the full (static) HTML of the page has been loaded; any dynamic content modifications that might be performed by JavaScript is not ensured to be complete, and will have to be waited for.
Hence my surprise that a static <title> tag in the HTML of my page did not always result in a non-empty return value from .getTitle.
If my reading of the documentation is incorrect, then what value does the *attempt* to block until the page has loaded really have?  If I'll have to *always* insert some explicit wait after .click()ing to make sure that my tests are reliably on a fully loaded page, then the utility of .click() doing an *attempt* to ensure this is ... rather small, I'd say.

 Assuming this is a valid problem:

·         Do you have a minimal test case that can regularly reproduce this behaviour?

The test case itself is rather small, but it's running against a non-publicly available web application.
 

·         Does this happen in all browsers (It’s possible the browser you are using is creating a <title> element and then populating it afterwards and WebDriver is getting the title text before the browser has finished rendering the <title> element in the DOM correctly)?


I've just tried with Firefox, and it appears that the problem is much more prevalent in older Firefox versions (I get failures ~50% of the time on Firefox 3) than in newer versions (I've so far seen a single failure on Firefox 10, and a colleague saw a single failure in Firefox 13; repeatedly running with Firefox 13 on my laptop has so far not managed to reproduce the failure there).
-- 
Harald

Mark Collin

unread,
Jun 28, 2012, 7:13:54 AM6/28/12
to seleniu...@googlegroups.com

 

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Harald Meland
Sent: 28 June 2012 11:45
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: getTitle intermittently does not return title

 

On Thursday, 28 June 2012 12:11:33 UTC+2, Mark Collin wrote:

The symptoms would suggest that the page is not fully loaded when you are getting the title.  As a result I would suggest you set an explicit wait to ensure that the page has loaded before trying to get the title.

That is exactly what was suggested on the bug report I mentioned... but I'd really like to know a bit more about when I can/can't trust WebDriver's heuristic for determining whether a page has loaded.  If I just go with your suggestion without having obtained this knowledge first, I suspect that I would make my tests significantly less readable by adding lots and lots of redundant explicit waits.

 

You will need somebody who knows exactly how that piece of code works to explain that to you, or you can check out the code base and investigate for yourself.

 

 I would point out that is says that WebDriver will *attempt* to block until the page has loaded.  It’s very hard (read almost impossible) to be 100% sure that a page has loaded unless you put in an explicit wait that checks for something on the page that is known to only be displayed once the page has finished loading.

I read the documentation as saying, in practice: WebDriver will wait until the full (static) HTML of the page has been loaded; any dynamic content modifications that might be performed by JavaScript is not ensured to be complete, and will have to be waited for.

Hence my surprise that a static <title> tag in the HTML of my page did not always result in a non-empty return value from .getTitle.

If my reading of the documentation is incorrect, then what value does the *attempt* to block until the page has loaded really have?  If I'll have to *always* insert some explicit wait after .click()ing to make sure that my tests are reliably on a fully loaded page, then the utility of .click() doing an *attempt* to ensure this is ... rather small, I'd say.

 

I would expect a static page to not cause any problems, usually timing problems are due to JavaScript either not finishing processing before WebDriver starts doing things with the page, or JavaScript changing things unexpectedly after WebDriver has started doing things with the page.

 

 Assuming this is a valid problem:

·         Do you have a minimal test case that can regularly reproduce this behaviour?

The test case itself is rather small, but it's running against a non-publicly available web application.

 

Can you reproduce it on a publically available applications, or anonymise the HTML of the page that is giving you a problem and make it available publically?

 

 ·         Does this happen in all browsers (It’s possible the browser you are using is creating a <title> element and then populating it afterwards and WebDriver is getting the title text before the browser has finished rendering the <title> element in the DOM correctly)?

 

I've just tried with Firefox, and it appears that the problem is much more prevalent in older Firefox versions (I get failures ~50% of the time on Firefox 3) than in newer versions (I've so far seen a single failure on Firefox 10, and a colleague saw a single failure in Firefox 13; repeatedly running with Firefox 13 on my laptop has so far not managed to reproduce the failure there).

 

·         What about other browsers, is this a FireFox only problem?

·         What are the machine specs? 

·         Are the problems only seen on less powerful machines?

 

The symptoms seem to indicate that WebDriver is starting checks against the title before the Browser has finished rendering the DOM.

 

This thread will probably be interesting reading for you:

 

https://groups.google.com/d/topic/selenium-developers/cZbJ6oX9S9s/discussion

 

 

-- 

Harald

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/mcVdoAlWt1AJ.

Harald Meland

unread,
Jun 29, 2012, 4:32:11 AM6/29/12
to seleniu...@googlegroups.com
On Thursday, 28 June 2012 18:53:20 UTC+2, Darrell Grainger wrote:
I'd be curious what happens if you use:

WebElement titleTag = waitForElement(By.tagName("title"));
String title = titleTag.getText();

The code for waitForElement can be found at  http://darrellgrainger.blogspot.ca/2012/01/waitforelement.html.

As your waitForElement helper always will wait 250 ms before it even tries to query the DOM, I'd guess it would fare somewhat better just because of that.

I've done 10 runs using the code above, and have so far not been able to reproduce the intermittent test failures with it (which doesn't really surprise me, as waitForElement is in fact a home-grown kind of explicit wait).
 
It does seem like the getTitle() has some sort of problem. If it could not find the TITLE tag I'd expect a NoSuchElementException.

The documentation for getTitle says that it should return null if the title "is not already set".  Although the documentation seems to differ from the current implementation (I get "" instead of null), I'd be even more surprised if I got a NoSuchElementException.

The other option is write a hack to work around the problem but make the hack so it will still work when the getTitle() method works as expected. You could wrap the getTitle() method to be:

My current workaround is a variant of this (with exponential backoff, and no sleep if the first getTitle() call does indeed produce a proper result).
-- 
Harald

Harald Meland

unread,
Jun 29, 2012, 8:02:24 AM6/29/12
to seleniu...@googlegroups.com
On Thursday, 28 June 2012 13:13:54 UTC+2, Mark Collin wrote:

I would expect a static page to not cause any problems, usually timing problems are due to JavaScript either not finishing processing before WebDriver starts doing things with the page, or JavaScript changing things unexpectedly after WebDriver has started doing things with the page.

Further investigation makes me suspect that there is some interaction between the JavaScript that runs on the page I want to get the title of, and .click()'s heuristics for determining when that page has finished loading -- which causes .click() to sometimes return too early, and in turn .getTitle to not work consistently (even though the page's title is part of the HTML returned from the initial GET, i.e. is static).

I've tried to narrow things down to a minimal set of JavaScript components that can trigger the flakiness: If the page looks like this:

<head>
  ...
  <script type="text/javascript" src="/js/less-1.0.41.min.js"></script>
</head>
<body>
  ...
  <script type="text/javascript" src="/js/jquery-1.6.2.min.js"></script>
  <script type="text/javascript" src="/js/jquery-ui-1.8.16.custom.min.js"></script>
  <script type="text/javascript" src="/js/jquery.dataTables.js"></script>
  <script type="text/javascript" src="/js/errorFlusher.js"></script>
</body>

(where errorFlusher.js is a small homegrown script that registers an XHR to be done on jQuery's $(document).ready), I get occasional errors (on Firefox 3.6.22).

If I comment out the errorFlusher.js script, I've so far not been able to reproduce any errors.
Alternatively, if I move the less.js script down from the <head> to just before jquery in the <body>, I've also been unable to reproduce any errors.

Can you reproduce it on a publically available applications, or anonymise the HTML of the page that is giving you a problem and make it available publically?

This far I've been unable to reproduce it on any publically available application; I've tried seleniumhq.org and www.w3.org.  If you have any suggestions for a public web site I can try, please let me know.

·         What are the machine specs? 

My laptop is a MacBook Pro with a 2.2 GHz Intel Core i7 CPU, 8GB RAM and SSD disk.  I'm running the application locally in jetty when testing.

·         Are the problems only seen on less powerful machines?

I don't think so; although my laptop isn't a top-of-the-line model, I don't think of it as a "less powerful machine". :-)
 

This thread will probably be interesting reading for you:

https://groups.google.com/d/topic/selenium-developers/cZbJ6oX9S9s/discussion

Yes, indeed.  Thanks!
-- 
Harald

Mark Collin

unread,
Jun 29, 2012, 11:52:02 AM6/29/12
to seleniu...@googlegroups.com

I haven’t looked at the code that works out that the page has finished loading so this is guesswork, but it would seem that WebDriver is starting to process the page when the jQuery(document).ready event is thrown rather than waiting for the JavaScript that is processed after the jQuery JavaScript to finish..   

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Harald Meland
Sent: 29 June 2012 13:02
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: getTitle intermittently does not return title

 

On Thursday, 28 June 2012 13:13:54 UTC+2, Mark Collin wrote:

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/cE6xor4l05QJ.

Message has been deleted

Darrell Grainger

unread,
Jul 3, 2012, 9:34:37 AM7/3/12
to seleniu...@googlegroups.com
You can always change the code around. Have it check for the element first then do the 250ms sleep if it is not found. Or you can reduce the sleep to something smaller but this means checking more frequently.

I noticed a while ago that the documentation is often trailing from the code. I tend to open the source code rather than read the java docs. This means when I do read the documentation, I'm reading it with the code right underneath the comment. I quickly noticed when there was a change to the method the documentation wasn't always updated. I just got into the habit of trusting the documentation for big picture viewing but look at the code when I have to get into the details.

Oh, if you are looking for a public website which might exhibit your problem, give google.com a shot. Search for something the check the title on the result page. They have a fair bit of javascript on their pages. If that doesn't work, gmail probably will but it has a whole whack load of issues due to how heavily it uses javascript.

Darrell

rabia bashri

unread,
May 30, 2019, 11:43:48 PM5/30/19
to Selenium Users

A BIG Thank you ,I have searched this for from 1 till 4 AM :( :(

John Doe

unread,
Jun 3, 2019, 7:37:11 AM6/3/19
to Selenium Users
A better way to go - Explicit Wait, so basically you can wait for the specific expected title for a defined time period, i.e. 10 seconds, and if the title fails to update within the given time frame - the test will be marked as failed, pretty much similar to AJAX websites testing or any other situation when the initial presence of the element is not guaranteed.   

WebDriverWait wait = new WebDriverWait(driver, 10);
wait
.until(ExpectedConditions.titleIs("your anticipated page title"));


Reply all
Reply to author
Forward
0 new messages