Skip to content
Advertisement

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.

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.

How can I repsslicate running a command in the browser command line, in 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. Picture2 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.

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:\UsersKDJDocumentsGameJoinerchromedriver.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. This is because I removed it, since this cookie allows others to log into my account.

enter image description here

Advertisement

Answer

You can use Selenium to do that. With selenium, you can programmatic emulate your browser behaviour. These are the steps:

  • Download chromedriver according to your OS. Unzip it to a directory directory e.g. /Users/me/Documents/MyPrograme/

  • Install selenium:

python -m pip install selenium
  • Usage example:
# 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.

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.

Updates

  • Attempting to overcome 404 in Selenium 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)
  • Emulate the process If the website involves login. Perform the steps a human being will take. E.g. One webpage, go through logging, find inputs and password elements and emulate typing and clicking/or keyboard events.

Updates II

  • Dealing with pop ups
    Use driver.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()
  • Look at examples in SO on how to switch
Advertisement