Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

79 views
Skip to first unread message

hariprasad

unread,
Dec 8, 2017, 5:19:58 AM12/8/17
to Selenium Users
Hi,
 I am looking for something like:
getElementByXpath(//html[1]/body[1]/div[1]).innerHTML

I need to get the innerHTML of elements using JS (to use that in Selenium WebDriver/Java, since WebDriver can't find it itself), but how?

I could use ID attribute, but not all elements have ID attribute.

[FIXED]

I am using jsoup to get it done in Java. That works for my needs.

David

unread,
Dec 8, 2017, 4:20:19 PM12/8/17
to Selenium Users
Btw, for those interested, you could do it with something like this

WebElement elem = driver.findElement(By.XPath("//html[1]/body[1]/div[1]"));
String innerHtml = (String) ((JavascriptExecutor) driver).executeScript("return arg[0].innerHTML;"elem);

Serguei Kouzmine

unread,
Dec 13, 2017, 11:32:54 PM12/13/17
to Selenium Users
hello 
You may also try 
 String value = driver.findElement(By locator).getAttribute(String attribute);
Reply all
Reply to author
Forward
0 new messages