launching chrome with extension running (python + selenium) how to keep browser open - python

I am currently trying to launch google chrome with a certain chrome extension using Selenium and Python. The script works partially, google chrome detects and launches with the extension I wish to add though after the browser launches it closes almost instantly (error code: "...selenium.common.exceptions.SessionNotCreatedException: Message: session not created"). How do I keep the browser open after chrome launches with the extension? The script does not read anything past the driver launch, "driver = webdriver.Chrome(options=options, executable_path=r'C:/Webdriver/chromedriver.exe')"
code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
unpacked_extension_path = '/path/to/unpacked/extension/''
options = Options()
options.add_argument('--load-extension={}'.format(unpacked_extension_path))
driver = webdriver.Chrome(options=options, executable_path=r'C:/Webdriver/chromedriver.exe')
driver.get('example.com')
I have tried adding "chrome_options.add_experimental_option("detach", True)" and/or "global driver."Any help would be very appreciated!
screen-capture:
https://gyazo.com/52d6d6e6cfa61f5d8660204d773b5d03

Related

Chrome browser closes after running selenium chrome webdriver

I'm currently learning Selenium 4.0 and have set up a basic script that will click a button on Python's website. I'm using a Chrome webdriver. But whenever I run my code, a chrome window opens to the Python website and then closes immediately. How do I keep it open?
The browser version and the webdriver version are the same, and I've even tried the Edge webdriver and reinstalling Chrome. I've even tried downloading a webdriver to my local directory, but that doesn't work either. Here's my current script:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get("https://www.python.org/")
print(driver.title)
submit = driver.find_element(By.ID, "submit")
submit.click()
After running, my terminal gives this message:
====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/user1/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver] found in cache
Welcome to Python.org
Process finished with exit code 0
Well, it is the correct behavior as it does everything you told it to do correctly. Infact you're not recieving any errors. After having executed the code, Chrome Driver got killed because the Python app finishes its execution
If you want the Browser opened by the Driver to stay open use Chrome option and add detach
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

How do I fix this error in selenium webdriver?

I am using selenium pyhthon to try and open amazon.com in google chrome, but I keep getting this error message when I run the code.
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
I have tried to search up this error on stackoverflow, but all I can find is Selenium gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac, which I cannot understand. I am using mac M1 and I have the chrome version 92.0.4515.107. My code is
from selenium import webdriver
chrome_driver_path = "/Users/asznee/selenium/chromedriver"
driver = webdriver.Chrome(chrome_driver_path)
driver.get("https://www.amazon.com")
See your Google chrome is not installed in default location which is
C:\Program Files\Google\Chrome\Application
it is installed :
C:\Users\asznee\Desktop\Downloads\Google Chrome\Application
right ?
so for solving this issue, your chromdriver.exe needs to know where Google chrome is installed :-
You can try the below code :-
options = webdriver.ChromeOptions()
options.binary_location = "/Users/asznee/Desktop/Downloads/Google Chrome/Application/chrome"
chrome_driver_binary = "/Users/asznee/selenium/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, options = options)
there is no need to specify the path of the chrome driver just keep the chrome driver in the project
just copy and paste in the project
and start initializing the web driver
simply start typing with
WebDriver driver = new ChromeDriver();
and enter the URL of the application
and follow the code
WebDriver driver = new ChromeDriver();
WebDriver is having unimplemented methods
here is chrome driver is implementing these unimplemented methods
chrome driver is just the driver which you're telling the selenium to run the test on the chrome browser.
you can download it on the
https://chromedriver.chromium.org/home
if you want to work with the firefox browser you use the gecko driver.

Python selenium headless start with extensions and xfvb

I'm trying to run a headless Chrome process with an extension1. I'm using WSL.
For testing purposes I can use one of two browsers:
Ubuntu: Google Chrome 86.0.4240.183
Windows: Version 86.0.4240.183 (Official Build) (64-bit)
For production purposes the Ubuntu's Chrome is the only viable option2.
The rest of the stack:
Python 3.6.9
selenium: 3.141.0
In order to overcome Chrome not being able to start headlessly with extensions I use PyVirtualDisplay.
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600), backend='xvfb')
display.start()
options = ChromeOptions()
options.add_extension('/home/plonca/selenium_draft/extensions.crx')
options.add_argument('--no-sandbox')
windows_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver.exe'
ubuntu_chrome_driver = '/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/chromedriver'
chrome_driver = webdriver.Chrome(executable_path=ubuntu_chrome_driver
, options=options
)
The problem occurs in the last line: if the windows_chrome_driver is set as executable_path everything is fine. If I set ubuntu_chrome_driver instead, I get an error:
WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://bmlddehkgnjdalnfaecgflmmeknlbohi/_generated_background_page.html
from tab crashed
I wonder what the cause of the error might be. How to make chrome_driver work in the headless environment?
1 The extension is needed so that I can authenticate via a pop-up window. The extension has been created according to instructions found here. Other options such as sending username and password in the URL or automating the click with AutoIt doesn't work.
2 Please note that the Chrome seems to be the only option since using Firefox is a matter of an unresolved issue.

Selenium in python - Browser closing immediately after open testprogamm

As soon as I run my program, google opens (so far so good) but this window closes immediately after the program has run. I already installed the chromdriver (also matches the version of the search engine) and put it in the script folder in Python. Can someone help me?
Here is my Code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://google.com")
You need to specify an excecutable path for a chrome driver to be able to open a browser.
download a driver based on your browser in your case it's Chrome from here: https://chromedriver.chromium.org/downloads
then driver = webdriver.Chrome(here you have to write the path of the driver),you can put it wherever you want.

Python-Selenium test script not working

I am trying to automation some action on a website using Python and Selenium, this is the sample code I am trying to run, from the Mozilla website for running the Firefox webdriver
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "/usr/bin/firefox-aurora"
driver = webdriver.Firefox(capabilities=caps)
driver.get("https://www.google.com")
driver.quit()
When running this code, the Firefox instance opens normally, and I can even use the instance as if I just opened Firefox normally, but the code 'stops' executing at the driver = webdriver.Firefox(capabilities=caps) line, I tried debugging the code with no luck, the whole execution seems to just stop at this line, and nothing after it is reached!
I am running Python3.5, Selenium version 2.53.6, I have the 'Wires' executable at the /usr/local/bin which is in the environment's PATH, I also have Firefox Aurora version 49.0a2 running on ArchLinux.
Thanks in advance.
[Update:]
I managed to get it to work after all using Firefox 46 (the normal version).

Categories

Resources