[英]How to replace web-request with html file python selenium?
I want to replace web-request with html file.我想用 html 文件替换 web-request。 Link to the file html code https://pastebin.com/BJbgXtg0
链接到文件 html 代码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问题是
print(driver.page_source)
只打印文件的一部分
<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?如何打印整个文件?
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:另一种方法是直接使用JS替换内容:
driver.execute_script(f"var ele=arguments[0]; ele.innerHTML = '{html_content}';", driver.find_element_by_tag_name('html'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.