AttributeError: 'Options' object has no attribute 'add_experimental_option' - python

hi guys I hope that you are having a great time.
so I'm having this problem when I'm tring to execute this script.
import selenium.webdriver as webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
url = "https://www.google.com/"
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
#Change chrome driver path accordingly
driver = webdriver.Chrome(
executable_path = r"C: \drivers\chromedriver.exe", chrome_options =chrome_options)
print (driver.title)
the results in cmd:
Traceback (most recent call last):
File "C:\Users\modaw\Desktop\firefox elo\hi.py", line 12, in <module>
opts.add_experimental_option('debuggerAddress', 'localhost:9222')
AttributeError: 'Options' object has no attribute 'add_experimental_option'

if you have the same problem try using this string with code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
opt=Options()
opt.add_experimental_option("debuggerAddress","localhost:8989")
driver = webdriver.Chrome(executable_path="chromedriver.exe",chrome_options=opt)
driver.get("http://google.com")

You need to change the third line to from selenium.webdriver.chrome.options import Options instead of from selenium.webdriver.chrome.options import options since the Options class is uppercase. Otherwise Python will not import it successfully.
Python names are case-sensitive.
Chrome WebDriver Options Methods

Related

'WebDriver' object is not callable

I am trying to call webdriver from another but its showing me typeError with webdriver is not callable
fileName: "call_folder1"
import sys
sys.path.insert(1,"/Users/akru/Desktop/AKRU-Automation/automation-scripts/Staging/test_folder1/")
import Login_yopmail
obj1=Login_yopmail.setUpDriver()
obj1.initiate_driver()
driver=obj1.driver()
class login_yopmail:
def access_url(self):
url="https://avaxdev.akru.co"
def entre_email():
print("Login yopmail")
pass
here is file containing webriver
#import unittest
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
class setUpDriver:
def initiate_driver(self):
WINDOW_SIZE = "1920,1080"
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--disable-setuid-sandbox')
service = ChromeService(executable_path=ChromeDriverManager().install())
self.driver = webdriver.Chrome(service=service , options=chrome_options)
driver=self.driver
return driver
I am trying to call webdriver initiated in another python filer from another directory

I tried using webdriver manager but i keep getting this error

I tried selenium automation with webdriver but I keep getting errors. Please help me fix the problem
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
browser = webdriver.Chrome("chromedriver.exe")
browser.get("https://python.org")
the Error I received:
File "C:\Users\LUCKY-PC\OneDrive\Desktop\Python\automation\selenium_test.py", line 2, in <module>
from webdriver_manager.chrome import ChromeDriverManager
File "C:\Users\LUCKY-PC\anaconda3\lib\site-packages\webdriver_manager\chrome.py", line 4, in <module>
from webdriver_manager import utils
File "C:\Users\LUCKY-PC\anaconda3\lib\site-packages\webdriver_manager\utils.py", line 8, in <module>
import requests
File "C:\Users\LUCKY-PC\anaconda3\lib\site-packages\requests\__init__.py", line 95, in <module>
from urllib3.contrib import pyopenssl
File "C:\Users\LUCKY-PC\anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py", line 109, in <module>
orig_util_SSLContext = util.ssl_.SSLContext
AttributeError: module 'urllib3.util' has no attribute 'ssl_'
Try with ChromeDriverManager().install():
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get("https://python.org")
It's better if you download the chrome driver and then provide the chrome driver path to code.
Go to your chrome browser and type chrome://settings/help. From here you can find your chrome version.
Then go to https://chromedriver.chromium.org/downloads to download a chrome driver that matches your chrome version.
Now give the path to of that driver in the code below.
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class Driver_Class(webdriver.Chrome):
def __init__(self, driver_path, teardown=False):
self.driver_path = driver_path
self.options = Options()
self.options.headless = False
self.driver = webdriver.Chrome(executable_path=self.driver_path,options=self.options)
self.options.add_argument('--ignore-certificate-errors')
self.options.add_argument('--ignore-ssl-errors')
self.teardown = teardown
os.environ['PATH'] += self.driver_path
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def get_driver(self):
return self.driver
driverObj = Driver_Class("chrome_driver_path")
driver = driverObj.get_driver()
Now you can use this driver for the rest of your program.

Selenium closes browser right after opening

I am trying to open Brave using Selenium (in Python). It actually opens but then immediately closes with the following errors appearing in the console:
[23340:9252:1107/063438.209:ERROR:os_crypt_win.cc(93)] Failed to
decrypt: The parameter is incorrect. (0x57)
[23340:9252:1107/063438.210:ERROR:brave_sync_prefs.cc(114)] Decrypt
sync seed failure
DevTools listening on
ws://127.0.0.1:53809/devtools/browser/ecce3b0e-2884-4173-bdab-2215a3d7f507
[23340:9252:1107/063438.480:ERROR:CONSOLE(1)] "[Shields]: Can't
request shields panel data for tabId: 2. Error: No tab url
specified", source:
chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd/out/brave_extension_background.bundle.js
(1)
I've done some searching but couldn't really find anything helpful.
Here is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Selen:
def __init__(self):
options = Options()
service = Service("C:/Auxiliary/chromedriver_win32/chromedriver.exe")
options.binary_location = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
self.driver = webdriver.Chrome(service=service, options=options)
self.driver.get("https://google.com")
Selen()
I'm using Windows 11.
Firstly, do you have chrome installed, and secondly, does it work if you do this?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Selen:
def __init__(self):
options = Options()
path = "C:/Auxiliary/chromedriver_win32/chromedriver.exe"
self.driver = webdriver.Chrome(executable_path=path, options=options)
self.driver.get("https://google.com")
time.sleep(20)
driver.close()
instance = Selen()
If it does work, try this to get it to work with brave,
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Selen:
def __init__(self):
options = Options()
options.binary_location = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
path = "C:/Auxiliary/chromedriver_win32/chromedriver.exe"
self.driver = webdriver.Chrome(executable_path=path, options=options)
self.driver.get("https://google.com")
time.sleep(20)
driver.close()
instance = Selen()

TypeError: WebDriver.__init__() got an unexpected keyword argument 'firefox_options' error using firefox_options as arguments in Selenium Python

I'm trying to create a script which downloads a file from a website and for this I want to change the download filepath. When I try to do this with the Firefox options it gives me this error:
TypeError: WebDriver.__init__() got an unexpected keyword argument 'firefox_options'
Code:
from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import time
options = Options()
options.add_argument("download.default_directory=C:\\Music")
browser = webdriver.Firefox(firefox_options=options, executable_path=r'C:\\selenium\\geckodriver.exe')
browser.get('https://duckduckgo.com/')
The browser option parameter firefox_options was deprecated in Selenium 3.8.0
Browser option parameters are now standardized across drivers as options. firefox_options, chrome_options, and ie_options are now deprecated
Instead you have to use options as follows:
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("download.default_directory=C:\\Music")
browser = webdriver.Firefox(options=options, executable_path=r'C:\\selenium\\geckodriver.exe')

Error with selenium for send_keys : str' object has no attribute 'send_keys'

I am going to scrape with selenium; I followed the instruction but I have an error regarding "send_key" when I am going to send my username and password :
runfile('C:/Users/thmag/untitled3.py', wdir='C:/Users/thmag')
C:\Users\thmag\untitled3.py:20: DeprecationWarning: use options instead of chrome_options
driver= webdriver.Chrome(driver_path, chrome_options = options)
Traceback (most recent call last):
File "C:\Users\thmag\untitled3.py", line 30, in <module>
user_ele.send_keys('MyEmail#gmail.com')
AttributeError: 'str' object has no attribute 'send_keys'
My code is as follow:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
driver_path = r"C:\\Program Files (x86)\\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver= webdriver.Chrome(driver_path, chrome_options = options)
url= "https://healthunlocked.com/"
driver.get(url)
loginpage= driver.find_element_by_id("sitebar-login-button")
loginpage.send_keys(Keys.ENTER)
user_ele = driver.find_element_by_xpath('//*[#id="email"]')
user_ele.send_keys('MyEmail#gmail.com')
user_ele.send_keys(Keys.ENTER)
pass_ele = driver.find_element_by_xpath('//*[#id="password"]')
pass_ele.send_keys('MyPass')
pass_ele.send_keys(Keys.ENTER)
time.sleep(10)
driver.quit()
Clear the placeholder, then send keys :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver= webdriver.Chrome(options = options)
url= "https://healthunlocked.com/"
driver.get(url)
# closing cookies thing
driver.find_element_by_id('ccc-notify-accept').click()
loginpage= driver.find_element_by_id("sitebar-login-button").click()
time.sleep(3)
user_ele = driver.find_element_by_id('email')
user_ele.clear()
user_ele.send_keys('MyEmail#gmail.com')
pass_ele = driver.find_element_by_xpath('//*[#id="password"]')
pass_ele.clear()
pass_ele.send_keys('MyPassword')
# submitting
driver.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/div/section/div[1]/form/button').click()
time.sleep(10)
driver.quit()
if you just want to login, you can use requests to send a post request
Use options instead of chrome options and set user_ele.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver= webdriver.Chrome(driver_path, options = options)
url= "https://healthunlocked.com/"
driver.get(url)
loginpage= driver.find_element_by_id("sitebar-login-button")
loginpage.send_keys(Keys.ENTER)
user_ele = driver.find_element_by_xpath('//*[#id="email"]')
user_ele.send_keys('MyEmail#gmail.com')
user_ele.send_keys(Keys.ENTER)
pass_ele = driver.find_element_by_xpath('//*[#id="password"]')
pass_ele.send_keys('MyPass')
pass_ele.send_keys(Keys.ENTER)

Categories

Resources