Can I use Selenium and Brave together? - python

Rencently I changed my web-browser to Brave, but I was working with chromedriver to automating some tasks
I've read some and I found this post says that the version of ChromeDriver and Brave have to match, but I don't find driver to my current version only for ChromeDriver 80.0.3987.106.
This is my Brave info: VersiĆ³n 1.4.96 Chromium: 80.0.3987.132 (Official Build) (64-bits).
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option = Options()
option.binary_location = '/usr/bin/brave-browser '
driver_path = '/home/usr_1/Downloads/ML/chromedriver'
driver = webdriver.Chrome(options = option, executable_path = driver_path)
driver.get('https://pypi.org/')

To open a brave Browsing Context using Selenium driven ChromeDriver you can use the following solution:
Code Block:
from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe'
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', options=option)
driver.get("https://www.google.com")

Related

Browser Close Immediately | Selenium

I start to learn selenium today. I install chrome driver and firefox driver accoding to version. I try with both chrome and firefox but browsers closes immediately. I also add drivers to PATH. I also try using direct path of driver
Chrome Version: 109.0.5414.120
Selenium Version: 4.8
Python Version: 3.8
Conda Version: 23.1.0
Code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__) # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")
Seems like it closes, because your code is done running.
You might try:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__) # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")
input("Press ENTER to exit\n")
Service need to get the path to the chromedriver.exe as following:
service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=service)
C:\webdrivers\chromedriver.exe here is the actual path to chromedriver.exe on my computer
To keep the browser open set options with detach = true, as wollowing:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=service)
from selenium import webdriver
# Selenium Version
print(webdriver.__version__)
# Instantiating the Chrome driver
chrome_browser = webdriver.Chrome()
# Opening the browser to Google website
chrome_browser.get("https://www.google.com")

Python script crashing after opening chrome webdriver

Here's my code:
from selenium import webdriver
from selenium.webdriver import Chrome, chrome
def startchrome(url):
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches',['enable-logging'])
driver = Chrome(options=options)
driver.get(url)
this is for backend.py
and I run chrome by importing backend to main script and running by:
backend.startchrome(url)
Chrome opens but main crashes (says is not responding)
Thanks
modify startChrome() method a bit :
def startchrome(url):
executable_path = r"C:\\Users\\Inc\\Desktop\\Selenium+Python\\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path, options=options)
driver.get("http://www.google.com")
replace google.com with url string

How to bypass the message-"your connection is not private" on non-secure page using Selenium?

I'm trying to interact with the page "Your connection is not private".
The solution of using options.add_argument('--ignore-certificate-errors') is not helpful for two reasons:
I'm using an already open window.
Even if I was using a "selenium opened window" the script runs non stop, and the issue I'm trying to solve is when my browser disconnects from a splunk dashboard and I want it to automatically connect again(and it pops the private connection window).
How do I click on "Advanced" and then click on "Proceed to splunk_server (unsafe)?
For chrome:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
If not work then this:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
options = webdriver.ChromeOptions()
options.add_argument('--allow-insecure-localhost') # differ on driver version. can ignore.
caps = options.to_capabilities()
caps["acceptInsecureCerts"] = True
driver = webdriver.Chrome(desired_capabilities=caps)
For firefox:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://cacert.org/')
driver.close()
If not work then this:
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities['acceptSslCerts'] = True
driver = webdriver.Firefox(capabilities=capabilities)
driver.get('https://cacert.org/')
driver.close()
Above all worked for me!
This is how i handle this problem:
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
ChromeOptions capability = new ChromeOptions();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
WebDriver driver = new ChromeDriver(capability);
This chrome option is the silver bullet for me:
chromeOptions.addArguments("--allow-running-insecure-content");
If you need more, Open chrome & paste this URL:
chrome://flags/
One will find all the options and their impact on the chrome.
Either of below 2 solutions worked for me using Python Chrome Selenium Webdriver:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
capabilities = DesiredCapabilities.CHROME.copy()
capabilities["acceptInsecureCerts"] = True
driver = webdriver.Chrome(desired_capabilities=capabilities)
And accepted solution:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)

--headless flag no longer works after upgrade to chrome 76/chromedriver 76

I am running python selenium tests using headless chrome. After updating to chrome Version 76.0.3809.87 and chromedriver version ChromeDriver 76.0.3809.68, the chromeOptions that I use (see code sample below)... no longer work. i.e. the browser is launched (non headless) and the resolution settings are also not working
Anyone else seeing this after upgrading to chromedriver 75/76?
chrome_options = {'args': ['headless', '--window-size=1920,1080', 'no-sandbox', '--dns-prefetch-disable','--disable-dev-shm-usage']}
capabilities = {'browserName': 'chrome', 'chromeOptions':chrome_options}
cls.driver = webdriver.Chrome(desired_capabilities=capabilities)
You don't need to use desiredCapabilities to pass these arguments you can use Options instead. I have tested and it works.
Google Chrome - 76.0.3809.87
ChromeDriver - 76.0.3809.68
Selenium - 3.141.0
Python - 3.7.2
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com')
driver.save_screenshot("screenshot.png")
driver.quit()
Check the screenshot obtained from the headless browser below.

Using selenium: How to keep logged in after closing Driver in Python

I want to get my Whatsapp web (web.whatsapp.com) logged in, at the second time opening the Whatsapp web on chrome driver. Following is my code based on Python need your help.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_path = r"chromedriver.exe"
options = Options();
options.add_argument("user-data-
dir=C:/Users/Username/AppData/Local/Google/Chrome/User Data");
#options.add_argument("--start-maximized");
driver = webdriver.Chrome(chrome_path,chrome_options=options);
#driver = webdriver.Chrome();
driver.get('https://web.whatsapp.com/')
I tried on my Mac, below code and it worked perfectly fine, I don't need to login again
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=/tmp/tarun")
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://web.whatsapp.com/')
driver.quit()
For window you can try changing the path as below
options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data")
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://web.whatsapp.com/')
driver.quit()
Here it is for Windows. Works perfect on Python 3.6

Categories