0

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?

1
  • maybe the page you expect is not loaded directly. if it's the case, try to use WebDriverWait. you can read this answer to know how to use it: stackoverflow.com/a/26567563/13123142 . use it with a selector that you are sure that it will be shown. Commented Jan 2, 2021 at 20:00

1 Answer 1

1

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'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.