def get_named_functions_list(url):
myscript = settings.get_js_code() # here I get script that I told above
tool.open(url)
while not tool.document.READY: # here I wait while the page will completely loaded
pass
js_result = tool.execute_from_console(myscript)
return list(js_result.values())
from selenium import webdriver
myscript = settings.get_js_code() # here I get content of *.js file
driver = webdriver.PhantomJS()
driver.get(url)
result = driver.execute_script(myscript)
driver.quit()
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
def execute_script_on_url(
page_url,
js_script,
load_wait_time=1,
script_wait_time=0
):
service = Service(ChromeDriverManager().install())
options = webdriver.ChromeOptions()
# Stop browser windows to pop up.
options.add_argument('--headless')
driver = webdriver.Chrome(service=service, options=options)
try:
# Open the web page and wait for it to load.
driver.get(page_url)
time.sleep(load_wait_time)
# Execute the JS script and wait for it to finish.
result = driver.execute_script(js_script)
time.sleep(script_wait_time)
return result
finally:
driver.quit()
该函数将page_url和js_script作为文本执行。您可以像这样将文件转换为文本:
script = open('./the-file-route.js', 'r').read()
然后您可以像这样使用该函数:(例如)
result = execute_script_on_url('https://www.google.com/', script)
如果脚本返回了某些内容,那么它将被存储在 result 变量中。例如,如果你的 JavaScript 代码如下所示(愚蠢的示例):