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")
Related
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)
Lately one of my python selenium script running on Raspi4 linux stops working properly. The same code worked well before and I'm trying to figure out the reason.
The example code below works without headless option, but it is stuck in starting Chromium. Does anyone encounter similar issue?
Chromium and chromedriver version is 97.0.4692.99.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
url = 'https://www.google.com'
options = Options()
options.add_argument('headless')
#options.add_argument('-headless')
#options.add_argument('--headless')
#options.headless = True
service = ChromeService(executable_path='C:\selenium\driver\chromedriver.exe')
chrome = webdriver.Chrome(service=service, options=options)
chrome.get(url)
chrome.save_screenshot('xx.png')
chrome.close()
Instead of using --headless argument, you need to use the headless property as follows:
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
Reference
You can find a couple of relevant detailed discussion in:
DeprecationWarning: use setter for headless property instead of set_headless opts.set_headless(headless=True) using Geckodriver and Selenium in Python
How to make Firefox headless programmatically in Selenium with Python?
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
I am sucesfully able to load my chrome profile using the flags:
user-data-dir as well as profile-directory, yet once the profile is loaded and the chrome window is actually open, no webpage appears. It simply gets stuck on a blank screen.
When I remove the code for the profile it is actually able to open the webpage stored in the login-url variable.
Tried updating to latest version of chrome (94.0.4606.81) and I also used the exact steps listed here to ensure I have the right chrome driver version.
I also did the obvious like making sure there are not any instances of chrome running in the background.
Code is as follows:
import os
from os.path import exists
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
headless = False
login_url = "https://google.com)"
def startChrome():
global headless
try:
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("----user-data-dir=C:/Users/ERIK/AppData/Local/Google/Chrome/User Data")
chrome_options.add_argument("--profile-directory=Profile 1")
global driver
driver = webdriver.Chrome(path+"/chromedriver.exe", options=chrome_options)
except:
print("Failed to start Chrome!")
input()
exit()
startChrome()
driver.get(login_url)
input()
The following successfully opens google.com for me.
Selenium Version 3.141.0
ChromeDriver Version 94.0.4606.61
Chrome Version 94.0.4606.71
from os import path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def getDriver(profile_directory, headless = False):
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
userDataDir = path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\User Data")
chrome_options.add_argument(f"--user-data-dir={userDataDir}")
chrome_options.add_argument(f"--profile-directory={profile_directory}")
return webdriver.Chrome("./chromedriver.exe", options=chrome_options)
driver = getDriver("Profile 2")
driver.get("https://google.com")
I am really bad at coding and am trying to cop a 3070. I tried to create this bot, and the browser opens, but the website link does not open (if that makes sense). Here's my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# For Chrome
chrome_options = Options()
chrome_options.add_argument(
"--user-data-dir=C:/Users/ajith/AppData/Local/Google/Chrome/User Data")
chrome_options.add_argument('--profile-directory=Profile 1')
browser = webdriver.Chrome(
"C:/Users/ajith/OneDrive/Desktop/bot/chromedriver.exe", chrome_options=chrome_options)
# Bestbuy 3090 Page
browser.get(
"https://www.bestbuy.com/site/nvidia-geforce-rtx-3070-8gb-gddr6-pci-express-4-0-graphics-card-dark-platinum-and-black/6429442.p?skuId=6429442")` ` `
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+.