Chrome not started with Python Selenium webdriver - python

I got this error when running my code. BTW, I am running this behind a VPN.
Can anyone tell me how can I fix it?
BTW, I try --headless, but weird that only the default profile is started with headless.
selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: exited normally. (unknown error:
DevToolsActivePort file doesn't exist) (The process started from
chrome location C:\Program Files\Google\Chrome\Application\chrome.exe
is no longer running, so ChromeDriver is assuming that Chrome has
crashed.)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import os
os.environ['WDM_SSL_VERIFY'] = '0'
os.environ["HTTPS_PROXY"] = "http://127.0.0.1:4780"
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
# options.add_argument('--headless')
options.add_argument(r"--user-data-dir=C:\Users\nuc\AppData\Local\Google\Chrome\User Data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=Profile 5') #e.g. Profile 3
# options.binary_location
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.switch_to.new_window('tab')
driver.get("https://www.youtube.com/")
driver.save_screenshot('ss.png')

Related

driver.get(url) not executing

I am trying to automate some web scrapping in chrome
Previously I was trying it with a different method where the chrome used to open in guest mode and it was working, here is the previous method
url = 'https://stackoverflow.com/'#Example chrome = webdriver.Chrome(ChromeDriverManager().install())
chrome.get(url)
But when I shifted the method, where the chrome would open logged in by default, it is showing problems, there is no error, but driver.get(url) is not working, infact any lines after webdriver.Chrome... are not working
Here is the code after changes
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\\Users\user\AppData\Local\Google\Chrome\User Data\Default")
url = "https://www.geeksforgeeks.org/"
driver = webdriver.Chrome(executable_path=r"C:\\Users\user\AppData\Local\Google\Chrome\Application\chrome.exe", options=options)
print("h")
driver.get(url)
This is where you made a mistake.
driver = webdriver.Chrome(executable_path=r"C:\\Users\user\AppData\Local\Google\Chrome\Application\chrome.exe", options=options)
you need to pass the PATH of the chrome driver Not the chrome browser installed path.
if you not sure path and your chrome browser compatibility then use below code.
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)

webdriver_manager opens google chrome instead of brave

webdriver_manager site has a code to start webdriver with brave, but instead of brave it opens it with google chrome. My selenium version 4.6.0 gave the following code for selenium 4 (I also tried the given codes for selenium 3) as it can be seen on the site, but the webdriver still opens with chrome
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))
driver.get("https://pypi.org/project/webdriver-manager/")
Okay, so you just want to start a driver with Brave browser instead of chrome? Here is how I do that simplely, keep in mind I'm on Mac. You need the binaryPATH to the application.
add_block_ext = "Path to .crx extension"
driverPath = 'chromedriver'
binaryPath = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
options = webdriver.ChromeOptions()
options.binary_location = binaryPath
options.add_extension(add_block_ext)
browser = webdriver.Chrome(executable_path=binaryPath, chrome_options=options)
browser.get("https://www.google.com")

WebDriverException: failed to wait for extension background page to load: xxx Selenium

When I try to add an extension on my script like for example "Selenium" I get this error message:
selenium.common.exceptions.WebDriverException:
Message: unknown error: failed to wait for extension background page to load: chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.html
from timeout: Timed out receiving message from renderer: 10.000
Code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
executable_path = "chromedriver.exe"
chrome_options = Options()
chrome_options.add_extension('MetaMask.crx')
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
Downgrading MetaMask version to 9.01 solved this, something must have changed in the most recent 10.x versions.

When trying to use a custom profile in Chrome with selenium webdriver in Python I keep getting this error

WebDriverException: Message: Can not connect to the Service /usr/lib/chromium/chromium
One of the only places which seemed to show you how, but that might only work on Windows. That's where I got the code from. How to load default profile in chrome using Python Selenium Webdriver?
My code.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/home/me/.config/chromium/Default") #Path to your chrome profile
w = webdriver.Chrome(executable_path="/usr/lib/chromium/chromium", chrome_options=options)
w.get("https://google.com")
The browser opens up, pauses, doesn't go to the URL, then gives me that error message.
This code stops giving me the error message, but my user data does not show up.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
config = "_config/"
chromedriver = config+"chromedriver"
options.add_argument("--profile-directory='Default'") #Path to your chrome profile
w = webdriver.Chrome(chromedriver, chrome_options=options)
w.get("https://google.com")
And if I used this executable path instead, with everything else being the same, it opens up the browser with all of the desired user data, but then gives this error. WebDriverException: Message: Service chromium unexpectedly exited. Status code was: 0 The browser stays open:
w = webdriver.Chrome(executable_path="chromium", chrome_options=options)

Splnter selenium headless with Chrome Canary (python)

I can drive a headless browser using selenium and Chrome Canary.
But I can't get it to work using Splinter.
Thanks in advance.
Here's what works.
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', chrome_options=chrome_options)
Here's what doesn't work:
from splinter import Browser
executable_path = {'executable_path':'/Applications/Google Chrome Canary/Contents/MacOS/Google Chrome Canary'}
B=Browser('chrome',**executable_path)
Incidentally Splinter DOES work with phantomjs
executable_path = {'executable_path':'/Applications/phantomjs/bin/phantomjs'}
B=Browser('phantomjs',**executable_path )
The error message is
WebDriverException: Message: 'Google Chrome Canary' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Here's the path, as perceived by os.os.environ['PATH']
/Users/jonschull-MBPR/miniconda2/bin:/Applications/Google Chrome Canary/Contents/MacOS/Google Chrome Canary:/Users/jonschull-MBPR/miniconda2/bin:/Users/jonschull-MBPR/anaconda/bin:/Users/jonschull-MBPR/Downloads/google-cloud-sdk/bin:/opt/local/bin:/opt/local/sbin:/Users/jonschull-MBPR/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/chromedrive:/opt/X11/bin:/usr/local/mongodb/bin
And by the way, I've tried escaping "Google\ Chrome\ Canary "
Well, according to issue in splinter's git you just need to upgrade to splinter 0.7.6+.

Categories

Resources