如何在 python 中从 Web 浏览器控制台运行命令? [英] How do I run command from web browser console in python?
问题描述
我最近尝试在 Web 浏览器中通过 python 运行命令,就像使用 chrome 开发人员工具一样.
I have recently been trying to run a command through python in a web browser, as if using chrome developer tools.
这在 chrome 中很容易做到,但我如何从 python 中做到呢?我不知道如何从 python 脚本复制它.我要运行的浏览器命令特定于我正在使用的网站,这就是我需要使用命令行的原因.
This is easy to do from chrome, but how would I do it from python? I have no idea how I would go about replicating this from a python script. The browser command I am wanting to run is specific to the website I am working with, which is why I need to use the command line.
如何在 python 3 中在浏览器命令行中重复运行命令?对不起,如果这是一个愚蠢的问题.谢谢!
How can I repsslicate running a command in the browser command line, in python 3? Sorry if this is a stupid question. Thanks!
我最终使用了硒,但遇到了一些问题.我要去的网站(roblox.com)在浏览器控制台中说我未经授权.我很确定我在登录时正确实施了身份验证 cookie.出于某种原因,我的一些数据没有加载,例如我的货币计数.
I ended up using selenium, but ran into some problems. The site I am traveling to( roblox.com ), says that I am Unauthorized in the browser console. I am pretty sure that I implemented the authentication cookies correctly though, as I am logged in. For some reason, some of my data just isn't loading, such as my currency count.
这可能不是问这样问题的地方,因为本论坛的成员对 roblox 一无所知,但也许这是我代码中的一个简单问题.
This probably isn't the place to ask a question like this, since the members of this forum know nothing of roblox, but maybe it's a simple problem in my code.
# my code
from selenium import webdriver
import time
driver = webdriver.Chrome('C:\\Users\KDJ\Documents\GameJoiner\chromedriver.exe')
options = webdriver.ChromeOptions()
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
URL = 'https://www.roblox.com/games'
driver.get(URL)
driver.add_cookie({'name' : '.ROBLOSECURITY', 'value' : Cookie})
driver.get(URL)
顺便提一下,您可能没有看到我定义了 Cookie 变量.这是因为我删除了它,因为这个 cookie 允许其他人登录我的帐户.
As a side note, you might not see me defining the Cookie variable. This is because I removed it, since this cookie allows others to log into my account.
推荐答案
您可以使用 Selenium
来做到这一点.使用 selenium,您可以编程模拟您的浏览器行为.这些是步骤:
You can use Selenium
to do that. With selenium, you can programmatic emulate your browser behaviour. These are the steps:
根据您的操作系统下载chromedriver.将其解压缩到一个目录目录,例如
/Users/me/Documents/MyPrograme/
Download chromedriver according to your OS. Unzip it to a directory directory e.g.
/Users/me/Documents/MyPrograme/
安装selenium
:
python -m pip install selenium
- 用法示例:
# example.py
from selenium import webdriver
# Start Chrome Driver
chromedriver = 'Users/me/Documents/MyPrograme/chromedriver'
driver = webdriver.Chrome(chromedriver)
# Open the URL you want to execute JS
URL = 'https://www.example.com'
driver.get(URL)
# Execute JS
driver.execute_script("console.log(`Hello from Python`)")
有关示例和详细信息,请阅读 selenium
的文档.
For examples and details read selenium
‘s documentation.
另一种方法是使用类似 RPA 的库,例如PyAutoGUI 或 RPA-Python,用于自动打开 chrome,发送键和命令以打开控制台,并将文本作为脚本命令发送.
Another way is to use RPA-like libraries, e.g. PyAutoGUI or RPA-Python, to automate opening of chrome, send keys and commands to open console and send texts as scripts-commands.
- 尝试克服 Selenium Chrome 中的 404
# Add headers
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
user_agent = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/39.0.2171.95 Safari/537.36')
opts.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome(chrome_options=opts)
- 模拟流程如果网站涉及登录.执行人类将采取的步骤.例如.一个网页,通过日志记录,查找输入和密码元素并模拟输入和点击/或键盘事件.
- 处理弹出窗口
使用driver.switchTo
弹出的任何框架. - Dealing with pop ups
Usedriver.switchTo
whatever frame that pops up.
# find element in your popup. Something like
dialog_name = 'Open Rebox?'
pop_element = driver.find_element_by_name(dialog_name)
pop = driver.switch_to.frame(pop_element)
# find what you need to click or use tab
# ...
pop.click()
# switch back to main
driver.switch_to.parent_frame()
- 查看 SO 中有关如何切换的示例
这篇关于如何在 python 中从 Web 浏览器控制台运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!