How to start Chrome Browser through Chromedriver and Selenium - python

I am getting error issues all of a sudden with selenium and the chromedriver. I haven't changed a single thing yet I am met with these error messages. The script literally worked hours ago and now without any tweaks its not working.
traceback (most recent call last):
File "email.py", line 3, in <module>
from selenium import webdriver
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 20, in <module>
import http.client as http_client
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 71, in <module>
import email.parser
File "C:\Users\Doe Labs\Desktop\Austin\Scripts\email.py", line 12, in <module>
options = webdriver.ChromeOptions()
Here is my corresponding code:
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
caps = DesiredCapabilities().CHROME
#caps["pageLoadStrategy"] = "eager"
options = webdriver.ChromeOptions()
options.add_argument(r'load-extension=C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper')
#options.add_argument('start-fullscreen')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(desired_capabilities = caps, executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver', chrome_options=options)
driver.get('http://www.doelabs.com/')
driver.maximize_window()
Even more strange is that when open new terminal, load python, and type from selenium import webdriver, i dont get any errors. But, when I navigate to the folder where the script lives, and load python and type from selenium import webdriver, i get the error message that shows up above. I hope this can give some insight into my current predicament.

A few words about the solution :
email is a reserved word / keyword in Python Language, avoid using the word email within user defined filename/methods/classes.
pageLoadStrategy as eager is yet to be implemented in ChromeDriver, use either none or normal instead as per your requirement.
To maximize the Chrome Browser Window instead of maximize_window() use the argument start-maximized through ChromeOptions()
To load an extension use ChromeOptions as follows :
options.addExtensions(new File("/path/to/extension.crx"));
Here are the four methods to initialize Chrome Browser through ChromeDriver :
Vanila Method :
from selenium import webdriver
driver = webdriver.Firefox(r'C:\path\to\chromedriver.exe')
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Arguments as ChromeOptions :
from selenium import webdriver
options = webdriver.ChromeOptions()
options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Capabilities as DesiredCapabilities :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "normal"
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Arguments as ChromeOptions and Capabilities as DesiredCapabilities :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "normal"
options = webdriver.ChromeOptions()
options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()

You might want to change
executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver',
to
executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver.exe',
You seem to have missed .exe, the extension of the executable file.

Related

How can I use touchAction with Selenium 4's w3c protocol?

When I use TouchActions in python, the Error is
Message: unknown command: Cannot call non W3C standard command while in W3C mode
With this, ActionChains class works well. This is my python code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions
mobile_emulation = { "deviceName": "Nexus 5" }
options = Options()
options.add_argument('--headless')
options.add_experimental_option('mobileEmulation', mobile_emulation)
driver = webdriver.Chrome(options=options)
driver.get("http://example.com/")
element = driver.find_element(By.TAG_NAME, 'body')
actions = TouchActions(driver)
actions.tap(element)
#actions.double_tap(element)
#actions.flick(10, 10)
actions.perform()
driver.quit()
selenium 4.1.0
ChromeDriver 97.0.4692.71
Google Chrome 97.0.4692.71
How can I fix it?

Selenium unable to find element by ID in headless mode

I'm trying to automate email printing with Selenium & Chrome Driver; for some reason, the snippet below works fine when I exclude the options=options argument (i.e. not in headless mode). When I switch headless on, even specifying window size it still is unable to find the element by ID.
Config
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
#-----------------DEFAULT CONFIG
options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
options.add_argument("--allow-insecure-localhost")
options.add_argument("--allow-insecure-content")
options.add_argument("--no-sandbox")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
PATH = '/Users/*********/2021/selenium/chromedriver'
driver = webdriver.Chrome(PATH,options=options)
Failing Block (last line)
google_login = 'https://accounts.google.com/Login#identifier'
driver.get(google_login)
sleep(2)
html = driver.page_source.strip()
# =============EMAIL BOX
user_name = driver.find_element_by_id('identifierId')
Error Traceback: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="identifierId"]"}
I've tried using WebDriverWait in a try/except which also doesn't help.
Screenshot captured in headless mode attached (line prior to the failing step)
see headless mode is bit fast so I would suggest you to use it with Explicit waits :
wait = WebDriverWait(driver, 20)
user_name = wait.until(EC.element_to_be_clickable((By.ID, "identifierId")))
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Refer here for more
Instead of
options.add_argument("--window-size=1920,1080")
Please try this.
options.add_argument('--window-size=1920x1080')

How to initiate selenium web driver with brave browser using python

My chromium version is 87.0.4280.88
My brave browser version is 87.0.4280.101
I tried a lot of codes but they didn't work.
Code:
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")
Error traceback:
Traceback (most recent call last):
File "c:\Users\MOHSEN\Desktop\test\Untitled-1.py", line 5, in <module>
driver = webdriver.Chrome(
TypeError: __init__() got an unexpected keyword argument 'options'
To initiate a brave Browsing Session using Selenium driven WebDriver 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")
Browser Snapshot:

DeprecationWarning: use options instead of chrome_options error using ChromeDriver and Chrome through Selenium on Windows 10 system

Trying to run this code on my windows 10 machine https://github.com/KalleHallden/reddit_automations/blame/master/movie-tickets.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from decouple import config
import time
# the way to locate the button or thing you want on a website in chrome is
# by pressing cmd + shift + c and then you can use your mouse to find the
# info on the element that you want and you can copy the full xpath.
options = webdriver.ChromeOptions()
# options.add_argument('--ignore-certificate-errors')
# options.add_argument('--incognito')
# options.add_argument('--headless')
driver = webdriver.Chrome("/Users/kalle/Downloads/chromedriver83", chrome_options=options)
driver.get(config('THEATRE_SITE'))
# for some odd reason you need to reload the site for it to load.
# possibly a bug of the theatre site
driver.get(config('THEATRE_SITE'))
time.sleep(3)
# select city
button = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[1]/div/div[2]/ul/li[1]/label/input')
button.click()
# save city
button = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[2]/span/button')
button.click()
time.sleep(2)
# proceed to tickets tab
button = driver.find_element_by_xpath('/html/body/div[1]/nav/div[2]/div[2]/div[1]/ul[1]/li[1]/a')
button.click()
time.sleep(2)
# select the movie you want (should be more specific than just selecting the first one but whateva)
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[2]/div/div/div/div[2]/div/div[2]/div[2]/div[2]/ul/li[1]/ul/li/div/div[1]/div/span[2]/a')
button.click()
time.sleep(1)
# select the time you want to go
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[1]/div/div/div/div[4]/section/div/div[2]/div[2]/ul/li/ul/li[1]/div/span/span[3]/span[2]/span')
button.click()
time.sleep(1)
# choose amount of people
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[2]/div/div/div/div/section/div/div[2]/div/button')
button.click()
time.sleep(2)
# choose seats
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[1]/div/div/div/div[2]/section/div[3]/div[2]/button')
button.click()
time.sleep(2)
# pay
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[1]/div/div/div/div[2]/section/div[4]/div/div[2]/button/span')
button.click()
I have all the correct programs installed:
running python 3.8
have chrome webdriver installed
running version 84 stable chrome web browser
When I run the program it opens chrome however I get a blank web page with nothing on there but some text which says "data:." that is all nothing else.
This is the error I get in VS code:
:\Users\user>python c:/chromedriver_win32/movie-tickets1.py
c:/chromedriver_win32/movie-tickets1.py:15: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', chrome_options=options)
DevTools listening on ws://127.0.0.1:29442/devtools/browser/872d4312-b51d-4a38-bc1b-3b80495950
Traceback (most recent call last):
File "c:/chromedriver_win32/movie-tickets1.py", line 17, in <module>
driver.get(config("https://www.google.co.uk"))
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\decouple.py", line 199, in __call__
return self.config(*args, **kwargs)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\decouple.py", line 83, in __call__
return self.get(*args, **kwargs)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\decouple.py", line 68, in get
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: https://www.google.co.uk not found. Declare it as envvar or define a default value.
Any help would be much appreciated, I am unsure why decouple is not working.
This error message...
DeprecationWarning: use options instead of chrome_options
...implies that in your program you have used chrome_options to initiate a Selenium driven ChromeDriver initiated google-chrome Browsing Context.
chrome_options is deprecated now and you have to use options instead as well as pass the absolute path of the ChromeDriver along with the extension.
Solution
As you are triggering your tests on a windows-10 system, effectively you line of code will be:
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', options=options)
It's ok!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
no, the error will remain if you cover it like this
browser = webdriver.Chrome(options=chrome_options)
it will be right
browser = webdriver.Chrome(options=options)
***Below code sample is from 2022 and uses Web Driver Manager (https://pypi.org/project/webdriver-manager/)
#Imports for Web Driver, Service, Web Driver Manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
#options reference to Chrome Options
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
#Pass options to the WebDriver with ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)

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