swor
swor

Reputation: 901

How to replace web-request with html file python selenium?

I want to replace web-request with html file. Link to the file html code https://pastebin.com/BJbgXtg0

My code

from selenium import webdriver

file_path = "Mark.html"
with open(file_path) as html_file:
    driver = webdriver.Chrome()
    html_content = html_file.read()
    print(html_content) # prints full file -- OK
    print("--------------------")
    driver.get("data:text/html;charset=utf-8,{}".format(html_content))
    print(driver.page_source) # prints only part of the file --- PROBLEM
    print("---------------------------")
    edu_raw = driver.find_elements_by_xpath("//div[@id='education']/div/div/div")
    print(edu_raw)

Problem is that print(driver.page_source) prints only part of the file

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Mark Zuckerberg</title><meta name="referrer" content="origin-when-crossorigin" id="meta_referrer"><style type="text/css" nonce="92Mfjw08">/*<![CDATA[*/.bi .bk .cd{color:</style></head><body></body></html>

How can print whole file?

Upvotes: 0

Views: 214

Answers (1)

Nic Laforge
Nic Laforge

Reputation: 1876

You need to load the file using:

driver.get("file://" + absolutePath)

Then you may retrieve the content using

driver.page_source

Another way is to replace the content directly using JS:

driver.execute_script(f"var ele=arguments[0]; ele.innerHTML = '{html_content}';", driver.find_element_by_tag_name('html'))

Upvotes: 1

Related Questions