Find element by tag name not working with Firefox driver

1,358 views
Skip to first unread message

Jon Spalding

unread,
Feb 6, 2009, 6:57:04 AM2/6/09
to webdriver
Hi all,

I've been trying to get the Firefox driver to find an element by tag
name using:

driver.findElement(By.tagName("span"));

This works in the HtmlUnit driver but the Firefox driver throws this
exception:

java.lang.RuntimeException: Could not parse "".
at org.openqa.selenium.firefox.Response.<init>(Response.java:50)
at
org.openqa.selenium.firefox.internal.AbstractExtensionConnection.nextResponse
(AbstractExtensionConnection.java:230)
at
org.openqa.selenium.firefox.internal.AbstractExtensionConnection.readLoop
(AbstractExtensionConnection.java:195)
at
org.openqa.selenium.firefox.internal.AbstractExtensionConnection.waitForResponseFor
(AbstractExtensionConnection.java:188)
at
org.openqa.selenium.firefox.internal.AbstractExtensionConnection.sendMessageAndWaitForResponse
(AbstractExtensionConnection.java:156)
at org.openqa.selenium.firefox.FirefoxDriver.sendMessage
(FirefoxDriver.java:293)
at org.openqa.selenium.firefox.FirefoxDriver.sendMessage
(FirefoxDriver.java:289)
at org.openqa.selenium.firefox.FirefoxDriver.findElement
(FirefoxDriver.java:246)
at org.openqa.selenium.firefox.FirefoxDriver.findElementByTagName
(FirefoxDriver.java:234)
at org.openqa.selenium.By$5.findElement(By.java:176)
at org.openqa.selenium.firefox.FirefoxDriver.findElement
(FirefoxDriver.java:186)
<snip>
Caused by: org.json.JSONException: A JSONObject text must begin with
'{' at character 0
at org.json.JSONTokener.syntaxError(JSONTokener.java:496)
at org.json.JSONObject.<init>(JSONObject.java:180)
at org.json.JSONObject.<init>(JSONObject.java:403)
at org.openqa.selenium.firefox.Response.<init>(Response.java:38)
... 36 more

So it seems the firefox extension is returning an empty response.
After debugging I found that the command "selectElementByTagName" is
being sent by the Java code to the Firefox extension. I used telnet to
connect to the extension and used this json (having got the right
context id with findActiveDriver):

Length: 1
{"context":"{615292ea-98d5-487a-b8a2-
b5c5860c3c9a} ?","commandName":"selectElementByTagName","parameters":
["span"]}

The telnet session then closes abruptly without an error.

I've tried finding with By.id and By.xpath and they work fine, as does
using "selectElementById" in the json string command.

I did a quick grep of the extension code and there's no reference to a
"selectElementByTagName" function in the javascript, which I expected
there to be as there's a "selectElementById" function.

Is this a bug or am I being a bit daft?

Cheers,
Jon

Simon Stewart

unread,
Feb 6, 2009, 8:45:14 AM2/6/09
to webd...@googlegroups.com
Hi Jon,

Are you using the latest download? Or the current HEAD revision? It'd
help me track down the error. It may also be useful to open up the
error console (Tools->Error Console) to see if there's a useful
message being output there.

Regards,

Simon

Jon Spalding

unread,
Feb 6, 2009, 9:02:41 AM2/6/09
to webdriver
Hi Simon,

I'm using release 685.

The error console reports:

Error: driver is undefined
Source File:
file:///home/jon/.mozilla/firefox/oc85ozno.WebDriver/extensions/fxdr...@googlecode.com/components/driver-component.js
-> file:///home/jon/.mozilla/firefox/oc85ozno.WebDriver/extensions/fxdr...@googlecode.com/components/socketListener.js
Line: 260

Cheers,
Jon

On Feb 6, 1:45 pm, Simon Stewart <simon.m.stew...@gmail.com> wrote:
> Hi Jon,
>
> Are you using the latest download? Or the current HEAD revision? It'd
> help me track down the error. It may also be useful to open up the
> error console (Tools->Error Console) to see if there's a useful
> message being output there.
>
> Regards,
>
> Simon
>
> On Fri, Feb 6, 2009 at 11:57 AM, Jon Spalding
>

Simon Stewart

unread,
Feb 6, 2009, 9:23:32 AM2/6/09
to webd...@googlegroups.com
Hi Jon,

Thanks. I'll see if I can figure out what's going on.

Simon

Jon Spalding

unread,
Feb 9, 2009, 10:38:54 AM2/9/09
to webdriver
Hi Simon,

I just switched to using trunk and the same thing happens.

Cheers,
Jon

On Feb 6, 2:23 pm, Simon Stewart <simon.m.stew...@gmail.com> wrote:
> Hi Jon,
>
> Thanks. I'll see if I can figure out what's going on.
>
> Simon
>
> On Fri, Feb 6, 2009 at 2:02 PM, Jon Spalding
>
> <jon.spald...@googlemail.com> wrote:
>
> > Hi Simon,
>
> > I'm using release 685.
>
> > The error console reports:
>
> > Error: driver is undefined
> > Source File:
> > file:///home/jon/.mozilla/firefox/oc85ozno.WebDriver/extensions/fxdri...@googlecode.com/components/driver-component.js
> > -> file:///home/jon/.mozilla/firefox/oc85ozno.WebDriver/extensions/fxdri...@googlecode.com/components/socketListener.js

Simon Stewart

unread,
Feb 9, 2009, 1:08:23 PM2/9/09
to webd...@googlegroups.com
Hi Jon,

Nice to know the problem is consistent. I'll make this a priority for
tomorrow's coding session.

Simon

Jon Spalding

unread,
Feb 9, 2009, 5:16:33 PM2/9/09
to webdriver
Hi Simon,

I ended up looking into this a bit more and found the problem.
Basically it only happens when calling findElement(By.tagName("blah"))
on WebDriver but not when it's calling it on WebElement. This is
because it translates to the command name "selectElementByTagName"
being sent which doesn't exist in the Firefox extension. I changed the
command to use "selectElementUsingTagName" which does exist and got a
bit further. This time it failed because of a couple of typos in the
javascript function which were easy to fix. There aren't any tests
around finding by tag name using the driver so I added a couple in
ElementFindingTest.

If I could work out how to upload a file to this group I would attach
a patch, but either I'm a bit thick or I'm not allowed to so I've
pasted the diff below.

There's also a problem with socketListener.js line 282 which tries to
call driver.window on an undefined object when it tries to return an
unrecognised command error. This is what causes the extension to throw
an exception instead of the returning the error message. The driver
variable is defined out of scope but I didn't know the best thing to
do there so thought I'd leave that bit up to you.

Cheers,
Jon

Index: common/test/java/org/openqa/selenium/ElementFindingTest.java
===================================================================
--- common/test/java/org/openqa/selenium/ElementFindingTest.java
(revision 781)
+++ common/test/java/org/openqa/selenium/ElementFindingTest.java
(working copy)
@@ -84,6 +84,24 @@
assertThat(element.isSelected(), is(false));
}

+ @Ignore(SAFARI)
+ public void testShouldfindAnElementBasedOnTagName() {
+ driver.get(formPage);
+
+ WebElement element = driver.findElement(By.tagName("input"));
+
+ assertNotNull(element);
+ }
+
+ @Ignore(SAFARI)
+ public void testShouldfindElementsBasedOnTagName() {
+ driver.get(formPage);
+
+ List<WebElement> elements = driver.findElements(By.tagName
("input"));
+
+ assertNotNull(elements);
+ }
+
public void
testShouldNotBeAbleTofindElementsBasedOnIdIfTheElementIsNotThere() {
driver.get(formPage);

Index: firefox/src/java/org/openqa/selenium/firefox/FirefoxDriver.java
===================================================================
--- firefox/src/java/org/openqa/selenium/firefox/FirefoxDriver.java
(revision 781)
+++ firefox/src/java/org/openqa/selenium/firefox/FirefoxDriver.java
(working copy)
@@ -201,11 +201,11 @@
}

public WebElement findElementByTagName(String using) {
- return findElement("selectElementByTagName", using);
+ return findElement("selectElementUsingTagName", using);
}

public List<WebElement> findElementsByTagName(String using) {
- return findElements("selectElementsByTagName", using);
+ return findElements("selectElementsUsingTagName", using);
}

public WebElement findElementByXPath(String using) {
Index: firefox/src/extension/components/firefoxDriver.js
===================================================================
--- firefox/src/extension/components/firefoxDriver.js (revision 781)
+++ firefox/src/extension/components/firefoxDriver.js (working copy)
@@ -245,7 +245,7 @@
respond.send();
};

-FirefoxDriver.prototype.selectElementsUsingTagName = function
(respond, name) {
+FirefoxDriver.prototype.selectElementUsingTagName = function(respond,
name) {
var doc = Utils.getDocument(this.context);

var elements = doc.getElementsByTagName(name);
@@ -259,7 +259,7 @@
respond.send();
};

-FirefoxDriver.prototype.selectElementsUsingTagName = function
(respond, using) {
+FirefoxDriver.prototype.selectElementsUsingTagName = function
(respond, name) {
var doc = Utils.getDocument(this.context);

var elements = doc.getElementsByTagName(name);

On Feb 9, 6:08 pm, Simon Stewart <simon.m.stew...@gmail.com> wrote:
> Hi Jon,
>
> Nice to know the problem is consistent. I'll make this a priority for
> tomorrow's coding session.
>
> Simon
>
> On Mon, Feb 9, 2009 at 3:38 PM, Jon Spalding
>

Simon Stewart

unread,
Feb 11, 2009, 10:08:04 AM2/11/09
to webd...@googlegroups.com
Hi Jon,

Thanks for the patch. I've applied it to trunk and it's now in at r784:

http://code.google.com/p/webdriver/source/detail?r=784

I'll have a bash at fixing the amazing collapsing error handling too :)

If you're accessing the group through email, you should just be able
to attach a file to your email to get it sent to the group.

Regards,

Simon
Reply all
Reply to author
Forward
0 new messages