How to convert a Javascript call to Selenium (to locate a WebElement)

536 views
Skip to first unread message

Tiếu Thủy

unread,
Nov 20, 2014, 10:09:58 AM11/20/14
to seleniu...@googlegroups.com
Dear all,

I have a website, and when I use Inspector tool in Firefox on this website, I can locate a web element by a Javascript call, such as:

document.getElementById("xxx").children[0] ....

but when I tried in Selenium Java 2.44.0:

WebElement e = driver.findElement (By.id ("xxx"));

it returns the exception NoSuchElement.

So, how can I locate the web element I already found in Javascript in Selenium?

Thank you very much

Xiang Dong

unread,
Nov 20, 2014, 10:19:07 AM11/20/14
to seleniu...@googlegroups.com
By.id is almost the same thing as document.getElementById. They all try to find an element from DOM tree with specified ID.

So, from this point, By.id should works. 

The difference is document.getElementById is performed manually, but selenium test must over 100 time faster than manually test.

can u check is it a race condition issue, maybe sleep for a while a add implicit wait before locate page element from selenium

--david


Date: Thu, 20 Nov 2014 07:09:58 -0800
From: dqvi...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] How to convert a Javascript call to Selenium (to locate a WebElement)
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/33a957b4-8b6a-49e2-a78f-7524f15da855%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tiếu Thủy

unread,
Nov 20, 2014, 10:26:23 AM11/20/14
to seleniu...@googlegroups.com
Sorry for my mistake, actually By.id ("xxx") works, but after that, how can I get the content of the element like in Javascript (which are: children[0], contentDocument, innerHTML)

For instance, in Javascript I called:

document.getDocumentById("xxx").children[0].contentDocument

How can I do the same in Selenium?

Thank you very much
Message has been deleted

Xiang Dong

unread,
Nov 20, 2014, 10:37:10 AM11/20/14
to seleniu...@googlegroups.com
Chilren[0] point to another page element, you can use XPATH  to locate it.

contentDocument and innerHTML can be retrieved by execute javascript on this element via web driver.

WebElement also contains one method getAttribute(String attName), it will help you to retrieve some basic attribute of the page element.

--david


Date: Thu, 20 Nov 2014 07:26:23 -0800
From: dqvi...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] How to convert a Javascript call to Selenium (to locate a WebElement)

Jim Evans

unread,
Nov 20, 2014, 10:38:25 AM11/20/14
to seleniu...@googlegroups.com
Using findElement is the way to go, then you can pass it into a JavaScript execution. Here's a sample:

WebElement element = driver.findElement("xxx");
String childText = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].children[0].textContent;");

However, if you're trying to get the contentDocument property, you're probably trying to find an iframe element, in which case, you can use driver.switchTo().frame(element), and execute a script like "return document.innerHTML;", again using JavascriptExecutor. To switch back to the top-level frame after execution, use driver.switchTo().defaultContent().

--Jim

Tiếu Thủy

unread,
Nov 20, 2014, 10:49:50 AM11/20/14
to seleniu...@googlegroups.com
Hello David and Jim

Thank you very much

Here is the full function call I used:

String text = (String)((JavascriptExecutor)driver).executeScript("return document.getElementById('editorcontainer').children[0].contentDocument.getElementById('outerdocbody').children[0].contentDocument.getElementById('innerdocbody').innerHTML");

The Javascript call works.

But within Selenium, there is error:

Exception in thread "main" org.openqa.selenium.WebDriverException: document.getElementById(...).children[0].contentDocument.getElementById(...).children[0].contentDocument.getElementById(...) is null
Command duration or timeout: 75 milliseconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'score-dell-laptop', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-39-generic', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=33.0, platform=LINUX, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 8396a2c6-315b-4612-bfd2-56fa90e2d72f
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:508)
    at fr.inria.coast.mute.testEtherpad.main(testEtherpad.java:44)
Caused by: org.openqa.selenium.WebDriverException: document.getElementById(...).children[0].contentDocument.getElementById(...).children[0].contentDocument.getElementById(...) is null
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'score-dell-laptop', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-39-generic', java.version: '1.8.0_25'
Driver info: driver.version: unknown
    at <anonymous class>.anonymous(http://152.81.3.91:9001/p/486891732 line 68 > Function:1:8)
    at <anonymous class>.handleEvaluateEvent(http://152.81.3.91:9001/p/486891732:68:20)
    at <anonymous class>.M(http://152.81.3.91:9001/p/486891732:9:2573)
    at <anonymous class>.O(http://152.81.3.91:9001/p/486891732:9:2982)
    at <anonymous class>.T(http://152.81.3.91:9001/p/486891732:9:3836)
    at <anonymous class>.W/g(http://152.81.3.91:9001/p/486891732:9:4402)
    at <anonymous class>.W(http://152.81.3.91:9001/p/486891732:9:4423)
    at <anonymous class>.Y(http://152.81.3.91:9001/p/486891732:9:4501)
    at <anonymous class>.bb(http://152.81.3.91:9001/p/486891732:9:5122)
    at <anonymous class>.bc(http://152.81.3.91:9001/p/486891732:9:5384)
    at <anonymous class>.bd(http://152.81.3.91:9001/p/486891732:9:5465)
    at <anonymous class>.c(http://152.81.3.91:9001/p/486891732:9:6356)
    at <anonymous class>.<anonymous method>(http://152.81.3.91:9001/p/486891732:14:13)
    at <anonymous class>.window.onload/<(http://152.81.3.91:9001/p/486891732:23:5)

Tiếu Thủy

unread,
Nov 20, 2014, 10:49:55 AM11/20/14
to seleniu...@googlegroups.com

Tiếu Thủy

unread,
Nov 20, 2014, 10:50:51 AM11/20/14
to seleniu...@googlegroups.com
Hello Jim

Could you give me a code example to use driver.switchTo () ...

I understand that it may be too much for a question, but I hope you can help


On Thursday, November 20, 2014 4:38:25 PM UTC+1, Jim Evans wrote:

Jim Evans

unread,
Nov 20, 2014, 11:10:09 AM11/20/14
to seleniu...@googlegroups.com
Sounds like you're needing to navigate into two nested iframes to get where you need to go. It would be more helpful to see a representative sample of the HTML you're trying to automate. Assuming the HTML looks something like this:

<html>
  <body>
    <!-- Yes, I know this isn't really how it looks. I'm forcing hierarchical elements to simulate the iframe -->
    <iframe id="editorcontainer">
       <html>
        <body>
           <iframe id="outerdocbody">
            <html>
              <body>
                <!-- this is the item you want the innerHTML of -->
              </body>
            </html>
          </iframe>
        </body>
      </html>
   </iframe>
  </body>
</html>

I'd probably do something like this:

// Switch to the first frame
WebElement containerFrame = driver.findElement("editorcontainer");
driver.switchTo().frame(containerFrame);

// Switch to the nested frame.
// Note that findElement is restricted to look within the currently "focused" frame.
WebElement outerDocBodyFrame = driver.findElement("outerdocbody");
driver.switchTo().frame(outerDocBodyFrame);

// Get the innerHTML property of the documentElement
// Note that likewise, JavaScript is executed in the context of the currently "focused" frame.
String content = (String)((JavascriptExecutor)driver).executeScript("return document.documentElement.innerHTML;");

// Return back to the top-level document
driver.switchTo().defaultContent();

Hope this helps. If this example doesn't exactly mimic the structure of the actual document you're automating against, you can use the concepts here to extrapolate the proper steps.

--Jim

Xiang Dong

unread,
Nov 20, 2014, 8:41:11 PM11/20/14
to seleniu...@googlegroups.com
document.getElementById(...).children[0], I think this element is an Iframe or frame. you can't treat frame or Iframe like this way. you need to switch to the frame firstly. 

driver.switchTo().frame(frameElement). Then you can find element in the frame and retrieve attribute from it


Date: Thu, 20 Nov 2014 07:48:44 -0800
From: dqvi...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] How to convert a Javascript call to Selenium (to locate a WebElement)

Tiếu Thủy

unread,
Nov 21, 2014, 3:42:09 AM11/21/14
to seleniu...@googlegroups.com
Dear David and Jim

I tried to use:

WebElement containerFrame = driver.findElement(By.id ("editorcontainer"));
driver.switchTo().frame(containerFrame);


but I received an exception:


org.openqa.selenium.NoSuchFrameException: Element is not a frame element: DIV

Jim Evans

unread,
Nov 21, 2014, 4:37:34 AM11/21/14
to seleniu...@googlegroups.com
Please carefully reread my reply. In it, I specified the HTML structure I was assuming your page had. In contained the assumption that if you were using contentDocument, that there were frames involved. I believe I mentioed that if your HTML structure did not match my assumed example, you would need to make adjustments to my sample code. Until and unless you indicate what the actual structure of your HTML is, I won't be able to offer any further troubleshooting advice, and it's unlik anyone else will either. There are just too many unknowns without that information.

Tiếu Thủy

unread,
Nov 21, 2014, 4:51:11 AM11/21/14
to seleniu...@googlegroups.com
Hello Jim

Yes, I understand that it is my task, not yours, and you helped too much so far.

Thank you very much

Xiang Dong

unread,
Nov 21, 2014, 4:54:41 AM11/21/14
to seleniu...@googlegroups.com
Do u really have a Frame on the web page? or it is just a DIV. Only frame has contentDocument. If it is just a DIV, using web driver to locate it and retrieve whatever you want to from javascript all getAttribute method


Date: Fri, 21 Nov 2014 00:42:09 -0800

Tiếu Thủy

unread,
Nov 21, 2014, 4:59:12 AM11/21/14
to seleniu...@googlegroups.com
Hello David and Jim

I am dealing with Etherpad (https://github.com/ether/etherpad-lite)

I am sorry that I provided insufficient information. This is the first time I deal with web programming, HTML and Javascript, DOM and Xpath and so on ...

PeterJeffreyGale

unread,
Nov 21, 2014, 5:03:48 AM11/21/14
to seleniu...@googlegroups.com
Which element are you try to find on the page at https://github.com/ether/etherpad-lite?

PeterJeffreyGale

unread,
Nov 21, 2014, 5:11:58 AM11/21/14
to seleniu...@googlegroups.com
Or are you saying that you are working with a page built with this tool?

Tiếu Thủy

unread,
Nov 21, 2014, 5:21:37 AM11/21/14
to seleniu...@googlegroups.com
Hello all (Jim, Peter and David)


After reviewing carefully Jim's instruction, I can get the content of the document.

Thank you very much for the intensive explanation.

Of course, I cannot do anything other than say thank you, but again thank you very much

Tiếu Thủy

unread,
Nov 21, 2014, 5:25:39 AM11/21/14
to seleniu...@googlegroups.com
Hello,

I understand that it is too much, but if possible, could you take a look at my another problem at:


Basically, I want to sendKeys() to Google Docs document. (I can get the content by calling getText (), but when I called sendKeys(), nothing happened)

Thank you very much
Reply all
Reply to author
Forward
0 new messages