Discussion:
[selenium-users] Is there a way to get element by XPath using JavaScript in Selenium WebDriver?
hariprasad
2017-12-08 10:19:58 UTC
Permalink
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
<http://kosmiktechnologies.com/selenium-training-in-hyderabad/>
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.
--
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-users+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/f043bc66-2ab1-4d8c-a505-baa21977aa44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David
2017-12-08 21:20:19 UTC
Permalink
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);
Post by hariprasad
Hi,
getElementByXpath(//html[1]/body[1]/div[1]).innerHTML
I need to get the innerHTML of elements using JS (to use that in Selenium
<http://kosmiktechnologies.com/selenium-training-in-hyderabad/>
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.
--
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-users+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/231dd71d-27da-4320-affb-fc714ed25bde%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Serguei Kouzmine
2017-12-14 04:32:54 UTC
Permalink
hello
You may also try
String value = driver.findElement(By locator).getAttribute(String
attribute);
Post by hariprasad
Hi,
getElementByXpath(//html[1]/body[1]/div[1]).innerHTML
I need to get the innerHTML of elements using JS (to use that in Selenium
<http://kosmiktechnologies.com/selenium-training-in-hyderabad/>
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.
--
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-users+***@googlegroups.com.
To post to this group, send email to selenium-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/5fcf47ce-a887-44e0-a3fe-61e15e1e1012%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...