I've written a script which opens Chromium and keeps it open for a set time, but I want to be able to set a proxy within the python script before opening Chromium. Is this possible? I've googled for some time, but I can't find how to do it.
Thanks in advance.
import os
from time import sleep
import webbrowser
import random
import time
timeDelay = random.randrange(57, 100)
def search():
#new browser object
chrome = webbrowser.get('chromium-browser')
#search engine startpoint
google = chrome.open_new("https://www.google.com")
if __name__ == "__main__":
sleep(0.5)
search()
time.sleep(timeDelay)
Related
I need help with this code:
import webbrowser
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('https://www.google.com/')
print('test browser')
it open chrome and visit the website, but don't print until I close the browser.
If I use this one:
import webbrowser
webbrowser.open('https://www.google.com/')
print('test browser')
It run default browser(brave), visit the website and print correctly.
How can I run X browser and print without need of close it to continue script?
You could create a new Thread and in that thread open the browser:
from threading import Timer
def open_browser():
webbrowser.open('https://www.google.com/')
Timer(1, open_browser).start()
print(“test”)
Everything that has to do with interaction of the browser should now be placed in def open_browser():
This code will set a timer for 1 millisecond and then execute the function within a seperate thread, so your code keeps executing.
I am building a Tkinter app with python that initializes multiple selenium webdrivers. The initial problem was that lots of chromedriver.exe instances were filling up user's memory, even after using driver.quit() (sometimes). So to get rid of this issue, when closing the tkinter app, I wrote this line os.system("taskkill /f /im chromedriver.exe /T"), that solves my problem, but, by using this, a command prompt instance is initiated that self kills almost instantly. The problem is that the user can see it and I find it kinda disturbing. Is there any way I could hide it? Or is there a workaround for my initial problem, that is user friendly?
Use both driver.close() and driver.quit() in your code in order to free memory.
driver.close()
driver.quit()
To reduce the memory footprint you should use ChromeDriver-Service:
First you start the service, then use it when creating new drivers, and finally stop the service before program exit.
Put the code below in chrome_factory.py and then:
on program start call chrome_factory.start_service()
to create new driver call chrome_factory.create_driver()
the service and drivers will be automatically stopped/quit at program exit.
Using this approach will result in only ever having single chromedriver.exe process.
# chrome_factory.py
import atexit
from os.path import expanduser
from typing import Optional
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
EXECUTABLE = expanduser("~/bin/chromedriver")
_chrome_service: Optional[Service] = None
def start_service():
global _chrome_service
_chrome_service = Service(EXECUTABLE)
_chrome_service.start()
atexit.register(_chrome_service.stop)
def create_driver() -> webdriver.Remote:
global _chrome_service
opts = webdriver.ChromeOptions()
opts.add_argument("--headless")
driver = webdriver.Remote(_chrome_service.service_url,
desired_capabilities=opts.to_capabilities())
atexit.register(driver.quit)
return driver
def main():
start_service()
for _ in range(20):
create_driver()
if __name__ == '__main__':
main()
I am trying to make a simple program that opens the web browser when you go to a specific URL in flask.
I am using nginx with uwsgi, to work with flask, running on ubuntu desktop 18.04.
from flask import Flask
import webbrowser
app = Flask(__name__)
#app.route("/test")
def test():
#this is where a new webbrowser should be opened:
webbrowser.open_new_tab("https://google.com")
return "test!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
I expect a new tab of the webbrowser to be opened on the server machine but nothing happens
Do you have a default browser set? You need a default set.
From webbrowser:
If the environment variable BROWSER exists, it is interpreted as the os.pathsep-separated list of browsers to try ahead of the platform defaults.
Another example from the same Python reference page demonstrates you need a window open to use open_new_tab() function:
Here are some simple examples:
url = 'http://docs.python.org/'
# Open URL in a new tab, if a browser window is already open.
webbrowser.open_new_tab(url)
# Open URL in new window, raising the window if possible.
webbrowser.open_new(url)
Ideally, You create a controller object specifying your browser of choice from the table in that link, such as "mozilla", "chrome", "safari" etc., then use the open_new_tab() function on that controller.
https://docs.python.org/3.5/library/webbrowser.html#browser-controller-objects
UPDATE:
So I tried this
import webbrowser
def main():
# this is where a new webbrowser should be opened:
webbrowser.open_new_tab("https://google.com")
return "test!"
if __name__ == "__main__":
main()
And I can open a new tab irrespective if a window is open.
So it works for a simple python script.
Are you saying that when you run you flask app, and try to do a GET request at http://yourip-or-domain-name/test your browser doesn't open?
(Assumption here is port 80 as you don't bind an explicit port in your app.run() call.
I'm launching IE using vagrant, it gets launched as a background process, here is the python code :
import pyautogui import webbrowser import time import psutil
while True:
url = "http://google.com"
wb = webbrowser.get("C:\Program Files\Internet Explorer\iexplore.exe")
wb.open(url, new=2)
Is there a way using powershell or python to open an instance of IE in foreground ?
I'm writing a Python Script using webbrowser module to automatically open the desired webpages.
The issue I'm facing is that I'm only able to open the webpages on different Browser windows and not on the same Browser window on different tabs.
Below is the code that I'm using.
#! /usr/bin/python -tt
import webbrowser
def main():
webbrowser.open('url1')
webbrowser.open('url2')
webbrowser.open('url3')
if __name__ == '__main__':
main()
I want to open all these links on the same web browser window on separate tabs and not on different browser windows.
Thanks :)
You need to use webbrowser.open_new_tab(url). For example...
import webbrowser
url = 'http://www.stackoverflow.com'
url2 = 'http://www.stackexchange.com'
def main():
webbrowser.open(url2) # To open new window
print('Opening Stack Exchange website!')
webbrowser.open_new_tab(url) # To open in new tab
print('Opening Stack Overflow website in a new tab!')
if __name__ == '__main__':
main()
In python 3.6, a complete answer will include both webbrowser.open_new() and webbrowser.open_new_tab() from the webbrowser docs.
import webbrowser
def main():
# print(webbrowser._browsers) # for Python 3.x to determine .get() arg
browser = webbrowser.get('firefox')
urls = ['url1', 'url2', 'url3']
first = True
for url in urls:
if first:
browser.open_new(url)
first = False
else:
browser.open_new_tab(url)
if __name__ == '__main__':
main()
Enjoy the code. +1 if it helped you out. Cheers!
Simply webbrowser.open_new_tab('url')