[英]How do I run command from web browser console in python?
I have recently been trying to run a command through python in a web browser, as if using chrome developer tools.我最近一直在尝试在 Web 浏览器中通过 python 运行命令,就像使用 chrome 开发人员工具一样。
This is easy to do from chrome, but how would I do it from python?这在 chrome 中很容易做到,但我如何从 python 中做到这一点? I have no idea how I would go about replicating this from a python script.
我不知道如何从 python 脚本复制它。 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.
我想要运行的浏览器命令特定于我正在使用的网站,这就是我需要使用命令行的原因。
How can I repsslicate running a command in the browser command line, in python 3?如何在 python 3 中在浏览器命令行中重复运行命令? Sorry if this is a stupid question.
对不起,如果这是一个愚蠢的问题。 Thanks!
谢谢!
EDIT: 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.
我要去的网站 (roblox.com) 说我在浏览器控制台中未经授权。
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.
我很确定我在登录时正确实施了身份验证 cookie。出于某种原因,我的一些数据没有加载,例如我的货币计数。
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.这可能不是问这样的问题的地方,因为这个论坛的成员对 roblox 一无所知,但也许这是我代码中的一个简单问题。
# 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)
As a side note, you might not see me defining the Cookie variable.作为旁注,您可能没有看到我定义了 Cookie 变量。 This is because I removed it, since this cookie allows others to log into my account.
这是因为我删除了它,因为这个 cookie 允许其他人登录我的帐户。
You can use Selenium
to do that.您可以使用
Selenium
来做到这一点。 With selenium, you can programmatic emulate your browser behaviour.使用 selenium,您可以编程模拟您的浏览器行为。 These are the steps:
这些是步骤:
Download chromedriver according to your OS.根据您的操作系统下载chromedriver 。 Unzip it to a directory directory eg
/Users/me/Documents/MyPrograme/
将其解压缩到目录目录,例如
/Users/me/Documents/MyPrograme/
Install selenium
:安装
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`)")
For examples and details read selenium
's documentation .有关示例和详细信息,请阅读
selenium
的文档。
Another way is to use RPA-like libraries, eg PyAutoGUI or RPA-Python , to automate opening of chrome, send keys and commands to open console and send texts as scripts-commands.另一种方法是使用类似 RPA 的库,例如PyAutoGUI或RPA-Python来自动打开 chrome,发送键和命令以打开控制台,并将文本作为脚本命令发送。
# 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
whatever frame that pops up.driver.switchTo
弹出的任何框架。# 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()
@Prayson W. Daniel @Prayson W. 丹尼尔
what about calling full for/while loops with Selenium?用 Selenium 调用完整的 for/while 循环怎么样? You cannot run a multi-line Javascript function within Selenium with 'execute_script()' or even "execute_js(..)".
您不能使用“execute_script()”甚至“execute_js(..)”在 Selenium 中运行多行 Javascript 函数。 Suggestiosn?
建议? @PraysonW.Daniel Thanks :3
@PraysonW.Daniel 谢谢:3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.