AttributeError: 'Service' object has no attribute 'process' Error in host - python

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

Related

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

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

Python selenium is neither not looking for id nor selecting button or giving input

So when I am execute my code the website will be open but the other steps which I described in my code below are not be execute, why? I even tried time.sleep() after the website is loaded in order to execute the remaining code but it is not working.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('/Users/User/Desktop/it_projects/python-google-automation/Neuer Ordner/chromedriver')
#open certain website
driver.get('https://www.nike.com/de/?cp=58194921917_search_%7cnike%7c10594878138%7c107792850434%7ce%7cc%7cDE%7cpure%7c452291007809&ds_rl=1252249&gclid=EAIaIQobChMIod-_o8jD-QIVE4XVCh1-FggsEAAYASAAEgJSYfD_BwE&gclsrc=aw.ds')
#go through cookie process
evade_cookie = driver.find_element_by_id('hf_cookie_text_moreInformation') #search for cookie-button (more informations)
evade_cookie.send_keys(Keys.RETURN) #click on button
select_cookie = driver.find_element_by_id('hf_cookie_label_done') #search for done-button
select_cookie.send_keys(Keys.RETURN) #click on done-button
#search for Sneakers
search = driver.find_element_by_id('VisualSearchInput') #search for input-area
search.send_keys('Nike Dunk Low') #insert input
search.send_keys(Keys.RETURN) #enter or return respectively
Error
/Users/user/Desktop/it_projects/python-google-automation/Neuer Ordner/main.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome('/Users/user/Desktop/it_projects/python-google-automation/Neuer Ordner/chromedriver')
Traceback (most recent call last):
File "/Users/user/Desktop/it_projects/python-google-automation/Neuer Ordner/main.py", line 14, in <module>
evade_cookie = driver.find_element_by_id('hf_cookie_text_moreInformation') #search for cookie-button (more informations)
AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'
user#MacBook-Air-von-Sami python-google-automation %
It seems that the syntax you are using is incorrect.
Try the below code
#go through cookie process
evade_cookie = driver.find_element('id','hf_cookie_text_moreInformation') #search for cookie-button (more informations)
evade_cookie.send_keys(Keys.RETURN) #click on button
select_cookie = driver.find_element('id','hf_cookie_label_done') #search for done-button
select_cookie.send_keys(Keys.RETURN) #click on done-button
The following code will properly wait for the elements to be clickable, and execute correctly. The setup is Firefox/geckodriver on Linux, but you can adapt it to your own, just observe the imports, and the part after defining the browser/driver:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
firefox_options = Firefox_Options()
driverService = Service('chromedriver/geckodriver')
browser = webdriver.Firefox(service=driverService, options=firefox_options)
url = 'https://www.nike.com/de/?cp=58194921917_search_%7cnike%7c10594878138%7c107792850434%7ce%7cc%7cDE%7cpure%7c452291007809&ds_rl=1252249&gclid=EAIaIQobChMIod-_o8jD-QIVE4XVCh1-FggsEAAYASAAEgJSYfD_BwE&gclsrc=aw.ds'
browser.get(url)
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="Mehr Informationen"]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="Fertig"]'))).click()
search_box = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,'VisualSearchInput')))
search_box.send_keys('Nike Dunk Low')
t.sleep(1)
search_box.send_keys(Keys.RETURN)
Selenium documentation: https://www.selenium.dev/documentation/
As per the error stacktrace there are two errors:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object: Implies that the default argument executable_path is deprecated now and you have to use an instance of the Service() class. See: DeprecationWarning: executable_path has been deprecated selenium python
AttributeError: 'WebDriver' object has no attribute 'find_element_by_id': Implies that the find_element_by_* commands are deprecated in the latest Selenium Python libraries. Instead you have to use find_element(). See: find_element_by_* commands are deprecated in selenium
Your effective code block will be:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service('/Users/User/Desktop/it_projects/python-google-automation/Neuer Ordner/chromedriver')
driver = = webdriver.Chrome(service=s)
driver.get('https://www.nike.com/de/?cp=58194921917_search_%7cnike%7c10594878138%7c107792850434%7ce%7cc%7cDE%7cpure%7c452291007809&ds_rl=1252249&gclid=EAIaIQobChMIod-_o8jD-QIVE4XVCh1-FggsEAAYASAAEgJSYfD_BwE&gclsrc=aw.ds')
evade_cookie = driver.find_element(By.ID, "hf_cookie_text_moreInformation")

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.

trying to send message on whatsapp with python selenium

I need to open web whatsapp but it gives me Deprecation Warning
here is the code
import selenium
from selenium import webdriver
import time
driver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(driver_path)
driver.get("https://web.whatsapp.com/")
driver.maximize_window()
time.sleep(20)
driver.quit
İts not giving me anything just opens Chrome
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
First you need download chromedriver - https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
ser = Service('path_to\chromedriver.exe')
browser = webdriver.Chrome(service=ser)
browser.get('https://web.whatsapp.com/')
browser.maximize_window()
time.sleep(20)
browser.quit
You need to pass The driver_path to Service(). below should work for you.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
driver_path = r"chromdriver.exe full file path here"
service = Service(driver_path)
option = webdriver.ChromeOptions()
driver = webdriver.Chrome(service = service, options = option )
driver.get("https://web.whatsapp.com/")
driver.maximize_window()
time.sleep(20)
driver.quit

Error while installing chrome extension through Python - selenium

I am trying to install chrome extension using Python Selenium.
When I click Add to chrome button a pop up(don't know whether it is a java scripted) is generated asking: "Add extension", "Cancel". I want to click "Add extension" but I am getting following error:
selenium.common.exceptions.NoAlertPresentException: Message: no alert open
My code:
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.implicitly_wait(30)
driver.get("https://chrome.google.com/webstore/detail/buyhatke/jaehkpjddfdgiiefcnhahapilbejohhj?hl=en")
time.sleep(15)
element=driver.find_element_by_css_selector("body > div.F-ia-k.S-ph.S-Rc-qa > div.h-F-f-k.F-f-k > div > div > div.e-f-o > div.h-e-f-Ra-c.e-f-oh-Md-zb-k >
div.dd-Va.g-c-wb.g-eg-ua-Uc-c-za.g-c-Oc-td-jb-oa.g-c")
element.click()
alert = driver.switch_to.alert
alert.accept()
help me to install it.
Updated code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import os
executable_path = "C:\\Users\\SACHIN\\AppData\\Local\\Programs\\Python\\Python36\\chromedriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension("C:\\Users\\SACHIN\\AppData\\Local\\Google\\chrome\\User Data\\Default\\Extensions\\jaehkpjddfdgiiefcnhahapilbejohhj\\
3.4.143_0")
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
This is because the download option pop up that you are trying to select using switch_to.alert is not actually a JS alert pop up. You can not select is using Selenium's switch_to methods. You need to use the DesiredCapabilities class to select this option and then use it in your code.
As per the ChromeDriver documentation, given here, please use the packed( the one with .crx extension ) or unpacked extension file (directory containing the extension, including a manifest.json file) and load it using DesiredCapabilities. This can be done using
from selenium.webdriver.chrome.options import Options
executable_path = "path_to_webdriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

Categories

Resources