简体   繁体   English

如何用 html 文件 python selenium 替换 web 请求?

[英]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.

相关问题 Python web scraping: How to combine selenium and pandas for gathering data on HTML? - Python web scraping: How to combine selenium and pandas for gathering data on HTML? 如何 web 使用 python Z8E00596AD8DE221328F8D8478D53 从 html 抓取嵌套表 - how to web scrape a nested table from html with python selenium 如何使用 python 替换 HTML 文件中的 HTML 代码? - how to replace HTML codes in HTML file using python? 如何使用 Python、Selenium 和 BeautifulSoup 将 html 保存到文本文件 - How to save html to a text file with Python, Selenium and BeautifulSoup 如何在python中使用selenium定位或提取html文件中的文本? - How to locate or extract texts in html file using selenium in python? 如何替换 Python 中大文件中的每个 HTML 部分? - How to replace every single HTML section in a large file in Python? 如何在 python/Selenium 中替换 URL 的“部分” - How to replace a "section" of the URL in python/Selenium 如何替换字符串中的特殊字符 - selenium python - How to replace special characters in string - selenium python 如何使用python硒从下面的HTML代码中选择特定的Web元素 - How to select the particular web element from the HTML code below using python selenium 如何在 html 中的标签之间添加标签 - How to add tags between tags in html using selenium web driver python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM