I\'m using the Python bindings to run Selenium WebDriver:
from selenium import webdriver
wd = webdriver.Firefox()
I know I can grab a webel
There is not really a straight-forward way of getting the html source code of a webelement
. You will have to use JS. I am not too sure about python bindings but you can easily do like this in Java. I am sure there must be something similar to JavascriptExecutor
class in Python.
WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);
Sure we can get all HTML source code with this script below in Selenium Python:
elem = driver.find_element_by_xpath("//*")
source_code = elem.get_attribute("outerHTML")
If you you want to save it to file:
with open('c:/html_source_code.html', 'w') as f:
f.write(source_code.encode('utf-8'))
I suggest saving to a file because source code is very very long.
And in PHPUnit selenium test it's like this:
$text = $this->byCssSelector('.some-class-nmae')->attribute('innerHTML');
WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);
This code really works to get JavaScript from source as well!
Java with Selenium 2.53.0
driver.getPageSource();
You can read innerHTML
attribute to get source of the content of the element or outerHTML
for source with the current element.
Python:
element.get_attribute('innerHTML')
Java:
elem.getAttribute("innerHTML");
C#:
element.GetAttribute("innerHTML");
Ruby:
element.attribute("innerHTML")
JS:
element.getAttribute('innerHTML');
PHP:
$element->getAttribute('innerHTML');
Tested and works with the ChromeDriver
.