Selenium python get innerhtml

Python Selenium: How to get the innerHTML from element

Solution 3: Sure we can get all HTML source code with this script below in Selenium Python: If you you want to save it to file: I suggest saving to a file because source code is very very long. Using page object in Java: Solution 4: A C# snippet for those of us who might want to copy / paste a bit of working code some day Thanks to those who answered before me.

Python Selenium: How to get the innerHTML from element

I want to get get the innerHTML from a element but when it run it get error say:

findtable.get_attribute('innerHTML') AttributeError: 'list' object has no attribute 'get_attribute' 
#findtable findtable = driver.find_elements(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td[1]/center/img") findtable.get_attribute('innerHTML') 

find_elements* returns a list , where as to identify a single element you need find_element* .

Additionally, get_attribute() is a WebElement method.

Solution

You need to change driver.find_elements() as driver.find_element() , you’ll be good to go.

Your effective code block will be:

findtable = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td[1]/center/img") findtable.get_attribute('innerHTML') 

instead of driver.find_elements() use driver.find_elements() without s at the end

findtable = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td[1]/center/img") findtable.get_attribute('innerHTML') 

Python Selenium: How to get the innerHTML from element, Browse other questions tagged python selenium selenium-webdriver innerhtml getattribute or ask your own question. The Overflow Blog The internet’s Robin Hood uses robo-lawyers to fight parking tickets and spam

Getting inner HTML with python’s selenium not working

I am trying to scrape a JS enabled webpage, but I am not able to access the HTML code that is seen in my web browser. I am successfully logging into and navigating to the relevant url. However, getting the inner html is not working.

from selenium import webdriver browser = webdriver.Chrome("path-to-webdriver") page = browser.get(url) inner_html = browser.execute_script("return document.body.innerHTML") print(inner_html) 

Below is the part of the HTML code that I want to become accessible; it is inside the first tags. The JS script generating the content is found below. The output of my python script contains no extra information compared to the HTML code presented below.

Читайте также:  Css точка или без

So, how I can get the inner HTML code of this page?

   

The part of the HTML I want is printed below, particularly the «Last update:» part.

  

 

 Jottonian time: 2018-02-26 09:24
Last update: 166:24 hours ago
Issues: Quite some 
news_page = browser.get(news_url) inner_html = wait(browser, 20).until(lambda browser: browser.find_element_by_id("divContent1").get_attribute("innerHTML").strip()) print(inner_html) 

If you want to get innerHTML which is generated dynamically you can try below code:

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait as wait browser = webdriver.Chrome("path-to-webdriver") page = browser.get(url) inner_html = wait(browser, 10).until(lambda browser: browser.find_element_by_id("divmyTrReport").get_attribute("innerHTML").strip()) wait(browser, 10).until(lambda browser: browser.find_element_by_id("divmyTrReport").get_attribute("innerHTML").strip() != inner_html) inner_html = browser.find_element_by_id("divmyTrReport").get_attribute("innerHTML") print(inner_html) 

This should allow you to wait up to 10 seconds (increase timeout if needed) until innerHTML of target div returned non-empty value

If you want to inner html like javascript you need to behave like javascript for example:

browser.execute_script('''document.getElementById("divmyTrReport").innerHTML = ' ';''') 

Selenium get inner html python Code Example, element = driver.find_element_by_xpath (‘//*’) element = element.get_attribute (‘innerhtml’) print html code of selected elemnt selenium python. python selenium find html. print element as html selenium python. selenium python get element html. selenium get full html of page. get html text …

How to get innerHTML of whole page in selenium driver?

I’m using selenium to click to the web page I want, and then parse the web page using Beautiful Soup .

Somebody has shown how to get inner HTML of an element in a Selenium WebDriver . Is there a way to get HTML of the whole page? Thanks

The sample code in Python (Based on the post above, the language seems to not matter too much):

from selenium import webdriver from selenium.webdriver.support.ui import Select from bs4 import BeautifulSoup url = 'http://www.google.com' driver = webdriver.Firefox() driver.get(url) the_html = driver---somehow----.get_attribute('innerHTML') bs = BeautifulSoup(the_html, 'html.parser') 

To get the HTML for the whole page:

from selenium import webdriver driver = webdriver.Firefox() driver.get("http://stackoverflow.com") html = driver.page_source 

To get the outer HTML (tag included):

# HTML from `` html = driver.execute_script("return document.documentElement.outerHTML;") # HTML from `` html = driver.execute_script("return document.body.outerHTML;") # HTML from element with some JavaScript element = driver.find_element_by_css_selector("#hireme") html = driver.execute_script("return arguments[0].outerHTML;", element) # HTML from element with `get_attribute` element = driver.find_element_by_css_selector("#hireme") html = element.get_attribute('outerHTML') 

To get the inner HTML (tag excluded):

# HTML from `` html = driver.execute_script("return document.documentElement.innerHTML;") # HTML from `` html = driver.execute_script("return document.body.innerHTML;") # HTML from element with some JavaScript element = driver.find_element_by_css_selector("#hireme") html = driver.execute_script("return arguments[0].innerHTML;", element) # HTML from element with `get_attribute` element = driver.find_element_by_css_selector("#hireme") html = element.get_attribute('innerHTML') 

driver.page_source probably outdated. Following worked for me

let html = await driver.getPageSource(); 

Using page object in Java:

 @FindBy(xpath = "xapth") private WebElement element; public String getInnnerHtml()

A C# snippet for those of us who might want to copy / paste a bit of working code some day

var element = yourWebDriver.FindElement(By.TagName("html")); string outerHTML = element.GetAttribute(nameof(outerHTML)); 

Thanks to those who answered before me. Anyone in the future who benefits from this snippet of C# that gets the HTML for any page element in a selenium test, please consider up voting this answer or leaving a comment.

Getting inner HTML with python’s selenium not working, from selenium import webdriver browser = webdriver.Chrome («path-to-webdriver») page = browser.get (url) inner_html = browser.execute_script («return document.body.innerHTML») print (inner_html) Below is the part of the HTML code that I want to become accessible; it is inside the first tags.

Get HTML source of WebElement in Selenium WebDriver using Python

I’m using the Python bindings to run Selenium WebDriver:

from selenium import webdriver wd = webdriver.Firefox() 

I know I can grab a webelement like so:

elem = wd.find_element_by_css_selector('#my-id') 

And I know I can get the full page source with.

But is there a way to get the «element source»?

The Selenium WebDriver documentation for Python are basically non-existent and I don’t see anything in the code that seems to enable that functionality.

What is the best way to access the HTML of an element (and its children)?

You can read the innerHTML attribute to get the source of the content of the element or outerHTML for the source with the current element.

element.get_attribute('innerHTML') 
elem.getAttribute("innerHTML"); 
element.GetAttribute("innerHTML"); 
element.attribute("innerHTML") 
element.getAttribute('innerHTML'); 
$element->getAttribute('innerHTML'); 

It was tested and worked with the ChromeDriver .

There is not really a straightforward way of getting the HTML source code of a webelement . You will have to use JavaScript. 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.

In Ruby, using selenium-webdriver (2.32.1), there is a page_source method that contains the entire page source.

How to get inner html of element selenium python Code, get innerhtml value in selenium python. get html text with selenium. how to get innerhtml of element selenium python. get command which fetched inner text of the element usingg selenium. element = driver.find_element_by_xpath (‘//*’) element = element.get_attribute (‘innerHTML’) robot code. can selenium …

Источник

Get HTML Source of WebElement in Selenium WebDriver using Python.

We can get html source of a webelement with Selenium webdriver.We can get the innerHTML attribute to get the source of the web element.

The innerHTML is an attribute of a webelement which is equal to the text that is present between the starting and ending tag. The get_attribute method is used for this and innerHTML is passed as an argument to the method.

Syntax

s = element.get_attribute('innerHTML')

We can obtain the html source of the webelement with the help of Javascript Executor. We shall utilize the execute_script method and pass arguments index.innerHTML and webelement whose html source is to be retrieved to the method.

Syntax

s = driver.find_element_by_id("txt-search") driver.execute_script("return arguments[0].innerHTML;",s)

Let us see the below html code of an element. The innerHTML of the element shall be − You are browsing the best resource for Online Education.

Example

Code Implementation with get_attribute.

from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe" # implicit wait applied driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # to identify element and obtain innerHTML with get_attribute l = driver.find_element_by_css_selector("h4") print("HTML code of element: " + l.get_attribute('innerHTML'))

Code Implementation with Javascript Executor.

from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe" # implicit wait applied driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # to identify element and obtain innerHTML with execute_script l = driver.find_element_by_css_selector("h4") h= driver.execute_script("return arguments[0].innerHTML;",l) print("HTML code of element: " + h)

Источник

Оцените статью