Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python selenium中设置页面源代码?_Python_Selenium - Fatal编程技术网

如何在python selenium中设置页面源代码?

如何在python selenium中设置页面源代码?,python,selenium,Python,Selenium,使用python selenium并加载网页时,我可以获得如下来源: webdriver.page_source 有没有办法设置页面源 我想从文件中“读取”html并对其执行定位操作,例如: driver = webdriver.Firefox() driver.set_source(open('my_file.html')) driver.find_element((By.XPATH, "//div[@id='create']//input")) 有办法吗?您可以直接打开文件 from s

使用python selenium并加载网页时,我可以获得如下来源:

webdriver.page_source
有没有办法设置页面源

我想从文件中“读取”html并对其执行定位操作,例如:

driver = webdriver.Firefox()
driver.set_source(open('my_file.html'))
driver.find_element((By.XPATH, "//div[@id='create']//input"))

有办法吗?

您可以直接打开文件

from selenium import webdriver
import os

driver = webdriver.Firefox()
driver.get('file:///' + os.getcwd() +'/my_file.html')
inputElement = driver.find_element_by_xpath("//div[@id='create']//input")
driver.quit()

另外,我记得这在IE上不起作用。它在Firefox和Chrome上运行良好。

您可以直接打开该文件

from selenium import webdriver
import os

driver = webdriver.Firefox()
driver.get('file:///' + os.getcwd() +'/my_file.html')
inputElement = driver.find_element_by_xpath("//div[@id='create']//input")
driver.quit()

另外,我记得这在IE上不起作用。它在Firefox和Chrome上运行良好。

您可以尝试实现以下功能:

# Get "html" element
current_html = driver.find_element_by_tag_name("html")
# Get "body" element from saved HTML doc
saved_doc = open("my_file.html")
new_body = saved_doc.read().split("<html>")[-1].split("</html>")[0]
# Replace "body" of current page with "body" of saved page
driver.execute_script("arguments[0].innerHTML = arguments[1]", current_html, new_body)

saved_doc.close()

您可以尝试实现如下内容:

# Get "html" element
current_html = driver.find_element_by_tag_name("html")
# Get "body" element from saved HTML doc
saved_doc = open("my_file.html")
new_body = saved_doc.read().split("<html>")[-1].split("</html>")[0]
# Replace "body" of current page with "body" of saved page
driver.execute_script("arguments[0].innerHTML = arguments[1]", current_html, new_body)

saved_doc.close()

你就不能打开这个本地文件吗?类似webdriver的东西。getfile:///path/my_file.htmlCouldn难道你不打开这个本地文件吗?类似webdriver的东西。getfile:///path/my_file.html