1

I want to use html code instead of real request.

My code

from selenium import webdriver
import json

with open("user1.html") as html_file:
    driver = webdriver.Chrome()
    html_content = html_file.read()
    driver.execute_script('document.write("{}")'.format(json.dumps(html_content)))
    # also I tried to change "" with ''
    # driver.execute_script("document.write('{}')".format(json.dumps(html_content)))

View user1.html here

I received error

javascript error: missing ) after argument list

I tried this, but it didn't help me Python: javascript error: missing ) after argument list

UPDATE Also I tried

from selenium import webdriver
import json

with open("user1.html") as html_file:
    driver = webdriver.Chrome()
    html_content = html_file.read()
    driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
    print(driver.page_source)

But this code prints <html><head></head><body></body></html>

3
  • Questions must be self contained. -- although it looks like that the answer is simply you have to escape JavaScript string, or find some way to pass it to JavaScript. Commented Jan 11, 2021 at 4:09
  • Does this answer your question? Escape character removed when using selenium execute_script through python Commented Jan 11, 2021 at 4:14
  • ... it looks like that you already find the solution; so make sure that you understand the solution completely (what it does) before applying to your code. What is your modified code? Commented Jan 11, 2021 at 4:22

0

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.