Skip to content

Instantly share code, notes, and snippets.

@dhruvkar
Last active November 14, 2023 14:26
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dhruvkar/dd29fbf535d19db69e979c748bfd3236 to your computer and use it in GitHub Desktop.
Save dhruvkar/dd29fbf535d19db69e979c748bfd3236 to your computer and use it in GitHub Desktop.
Run Browser in a Virtual Display using Selenium and transfer session to Python Requests
"""
If you use Chrome, get Chromedriver and put in your PATH:
http://chromedriver.chromium.org/downloads
If you use Firefox, get Geckodriver and put in your PATH:
https://github.com/mozilla/geckodriver/releases
Also install:
pip install requests
pip install selenium
pip install PyVirtualDisplay
"""
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException, UnexpectedAlertPresentException
from pyvirtualdisplay import Display
class Browser(object):
# initialize with 'chrome' or 'firefox' as the driver
def __init__(self, flavor):
self.display = Display(visible=0, size=(2880, 1800)).start()
if flavor.lower() == "chrome":
self.driver = webdriver.Chrome()
elif flavor.lower() == "firefox":
self.driver = webdriver.Firefox()
else:
raise Exception("need to specify 'chrome' or firefox' when creating a Browser")
def transfer_to_requests(self):
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
}
self.session = requests.session()
self.session.headers.update(headers)
for cookie in self.driver.get_cookies():
c = {cookie['name']: cookie['value']}
self.session.cookies.update(c)
return self.session
# example usage
b = Browser("chrome")
b.driver.get("https://www.google.com")
@007-JB
Copy link

007-JB commented Feb 7, 2021

'sesh' (line 48) supposed to be 'self' surely?

@dhruvkar
Copy link
Author

dhruvkar commented Feb 7, 2021

Yes!

It should be self.session. Fixed

@007-JB
Copy link

007-JB commented Feb 8, 2021 via email

@dhruvkar
Copy link
Author

dhruvkar commented Feb 8, 2021

Congrats man! They're growing up so fast! She's already 1.5 now.

Hope all is well with you and thanks for the kind words :)

-Dhruv

@aryan-cloud
Copy link

pyvirtualdisplay.abstractdisplay.XStartError: Xvfb program closed.

I am facing this error

  • Aryan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment