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.
Related
I want to webscraping with python in a host without Gui
and because this webpage (https://aqms.doe.ir/App/) don't access to scrap with requests library, I have to do it by selenium in headless webdriver. (Sorry! My English is bad!)
so I run this code in a cpanel host by terminal:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
with Chrome(options=chrome_options) as driver:
driver.get("https://aqms.doe.ir/App/")
sleep(10)
refresh = driver.find_element(By.XPATH, '/html/body/app-root/app-toolbar/div/div/app-toolbar-port/mat-toolbar/mat-toolbar-row[1]/button[2]/span/mat-icon')
refresh.click()
sleep(10)
shn = driver.find_element(By.CSS_SELECTOR, '#highcharts-29 > div > div:nth-child(1) > span > div > span:nth-child(1)').text
print (shn)
NOETS:
I have installed selenium in the host with terminal.
and the chromewebdriver.exe is in the folder that code is there (the source and chromewebdriver are in one folder).
I run this code with creating an application in cpanel by 'setup python' and runed it in the terminal.
but I got this Error:
File "req2.py", line 10, in <module>
with Chrome(options=chrome_options) as driver:
File "/home/gigachad/virtualenv/python_bot/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
super().__init__(
File "/home/gigachad/virtualenv/python_bot/3.8/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 103, in __init__
self.service.start()
File "/home/gigachad/virtualenv/python_bot/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 106, in start
self.assert_process_still_running()
File "/home/gigachad/virtualenv/python_bot/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 117, in assert_process_still_running
return_code = self.process.poll()
AttributeError: 'Service' object has no attribute 'process'
I think it's because of running it in the host.
thanks a lot!
You need to pass a Service object as an argument to Chrome() pointing to the installation location of the chromedriver binary object as follows:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument("--headless")
with Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) as driver:
driver.get("https://aqms.doe.ir/App/")
sleep(10)
refresh = driver.find_element(By.XPATH, '/html/body/app-root/app-toolbar/div/div/app-toolbar-port/mat-toolbar/mat-toolbar-row[1]/button[2]/span/mat-icon')
refresh.click()
sleep(10)
shn = driver.find_element(By.CSS_SELECTOR, '#highcharts-29 > div > div:nth-child(1) > span > div > span:nth-child(1)').text
print (shn)
References
You can find a couple of relevant detailed discussions in:
DeprecationWarning: executable_path has been deprecated selenium python
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
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
I'm trying to webscrape images from the web using selenium. On Linux there do not seem to be any problems however when running on windows the program seems to exit on line 18.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
import os
import requests
import base64
def webscrape(query, max_count, q):
options = Options()
options.add_argument('-headless')
# Add a GH_token to the environment so there are no limitations on the number of requests.
os.environ['GH_TOKEN'] = "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"
gecko = GeckoDriverManager().install()
driver = webdriver.Firefox(service=Service(gecko), options=options)
driver.get("https://www.google.com/search?q=" + query + "&tbm=isch")
print("test")
the test print doesn't happen. But all print statements before the driver.get call do.
from selenium import webdriver
import time
import random
numara = input("numarayi yaz ")
mesajSayisi = int(input("kac mesaj "))
sayi = random.randint(10000, 1000000)
sayi = str(sayi)
id = 'asdasd' + sayi
driver = webdriver.Chrome("D:\\Users\mutam\Desktop\sms_metin2\chromedriver.exe")
url = "https://www.hisarmt2.com/kayit-ol"
driver.Chrome.get(url)
when i run this code i get this error, anyone can help please?
driver = webdriver.Chrome("D:\\Users\mutam\Desktop\sms_metin2\chromedriver.exe")
Traceback (most recent call last):
File "D:\Users\mutam\Desktop\sms_metin2\main.py", line 9, in <module>
driver = webdriver.Chrome("D:\\Users\mutam\Desktop\sms_metin2\chromedriver.exe")
I'm not sure what your exact issue is... In the newer versions of Selenium the driver path is depreciated. Try installing webdriver_manager and using this below.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager #install webdriver_manager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get('website')
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()