Re: want to get an innerHTML through xpath

4,661 views
Skip to first unread message

David

unread,
Dec 10, 2012, 2:07:27 PM12/10/12
to seleniu...@googlegroups.com
Are you referring to Selenium RC? Your syntax is off as well, missing another pair of parenthesis. The correct RC method is something like this, using the browserbot object to access javascript DOM properties/attributes (e.g. innerHTML), which aren't natively exposed to Selenium for getAttribute().

selenium.getEval("this.browserbot.findElement(\"(//div[@id='searchtextPanel_wrap']/div[4]/div[4])[0]\").innerHTML");

If you were using WebDriver / Selenium 2, it's easier as

driver.findElement(By.xpath("(//div[@id='searchtextPanel_wrap']/div[4]/div[4])[0]")).getAttribute("innerHTML");

WebDriver gives you direct access  to javascript DOM properties/attributes. 

On Monday, December 10, 2012 5:37:06 AM UTC-8, sam wrote:

Hi All,
I am new to selenium, i want to get an innerHTML from my page. i didn't have div Id, so i am trying to get this through xpath. my xpath is given below
//div[@id='searchtextPanel_wrap']/div[4]/div[4]

Its working when i check this on firefox console with given below syntax
$x("//div[@id='searchtextPanel_wrap']/div[4]/div[4]")[0].innerHTML;

but cant with java.
selenium.getHtmlSource("//div[@id='searchtextPanel_wrap']/div[4]/div[4]")[0].innerHTML;

David

unread,
Dec 10, 2012, 7:58:59 PM12/10/12
to seleniu...@googlegroups.com
Made a typo earlier, for RC, the correct syntax is probably this as you have to explicitly mention locator is Xpath:

selenium.getEval("this.browserbot.findElement(\"xpath=(//div[@id='searchtextPanel_wrap']/div[4]/div[4])[0]\").innerHTML");

sam

unread,
Dec 11, 2012, 3:52:27 AM12/11/12
to seleniu...@googlegroups.com
Thanks David you giving me time. Its not working with the both strings you giving to me, showing error is
showException(e): ERROR: Threw an exception: Element xpath=(//div[@id='searchtextPanel_wrap']/div[4]/div[4])[0] not found

But the xpath is correct and giving me output when execute on firefox console.Yes i am using selenium RC 2.25.0
waiting for your reply.

David

unread,
Dec 11, 2012, 10:36:36 PM12/11/12
to seleniu...@googlegroups.com
Then I suggest that in your test, set a breakpoint where you work with the XPath, so that it stops there and you can debug from there, and also run your test with a custom Firefox profile that has Firebug installed. That way on breakpoint, you can open up Firebug at that screen to see if it can detect XPath at that point.

Either the page is misbehaving when running via Selenium, or it's a timing problem (too fast, etc. but ok when testing manually with Firebug), or it's one of those strange locator issues where it works in Firebug but not Selenium.

One other thing you can try is running Selenium in a shell/interpreter mode and then execute selenium commands there to see if it can find that XPath because in shell mode, Selenium will run slower than a test and rule out any timing related issues so you can focus and fix/tweak that to get it working rather than pulling your hair trying to figure out the problem.

You can run Selenium in shell mode by following these post examples:


Hope that helps.
Reply all
Reply to author
Forward
0 new messages