![](/img/trans.png)
[英]is it possible to run a javascript function on a website by using the url of the website?
[英]Would it be possible to open a website and then run JavaScript in it using Python?
我一直在嘗試打開一個網站(使用 python),然后在其上運行 JavaScript 腳本。
我做了打開網站部分,但我沒有設法讓 JavaScript 在頁面加載后運行。
import os
url='example.com'
jscode='alert(\'success\')'
os.system('start chrome.exe '+url+' javascript:'+jscode)
上面的代碼是我能想到的最好的。
它會打開網站,但不會運行 javascript。
我使用os.system()
是因為其他方法對 chrome 有問題。
我想到了:
VBScript 和批處理一起打開網站然后輸入擊鍵
' https://www.codegrepper.com/code-examples/whatever/Make+a+batch+file+that+opens+site+in+browser+and+enter+login+information '對於開發這個答案非常有用
'''
enclosed in '{}' means special characters
'~' or '{ENTER}' = [ENTER]
'^' = [CTRL]
multiple characters in the same list item are typed at the same time
'''
keyplan = ["^l","javascript:alert{(}'success'{)}","~"]
with open('temp.cmd','w') as f: pass
with open('temp.cmd', 'a') as f:
#batch code section
f.write(
'@if (@CodeSection == @Batch) @then\n'
+'@echo off\n'
+'set SendKeys=CScript //nologo //E:JScript "%~F0"\n')
+'START CHROME "example.com"\n'
+'timeout /t 10\n'
)#the timeout is to give time for the page to load
#log keys in plan
for key in keyplan:
f.write('%SendKeys% "'+key+'"\n')
f.write('goto :EOF\n')
f.write('@end\n')
#VBScript section
f.write('var WshShell = WScript.CreateObject("WScript.Shell");\n')
#send keys (runs on every '%SendKeys% "key")
f.write('WshShell.SendKeys(WScript.Arguments(0));\n')
os.system("temp.cmd")
寫完這篇文章后,我意識到import pynput
是一種不涉及os.system
的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.