why I get __init__.py problems when using selenium webdrivers chrome? - python

im working on a project and have this as code:
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
print(driver.current_url)
but i get an error and i dont know how to fix it, well mutle tracebacks to error i think, here is the error:
Traceback (most recent call last):
File "C:\Users\ytty\PycharmProjects\test hack\main.py", line 2, in <module>
from selenium import webdriver
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 36, in <module>
from .service import Service
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\service.py", line 21, in <module>
class Service(service.Service):
AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'
Process finished with exit code 1
can anyone help?

I do it this way:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=ChromeDriverManager().install())
This way it will automatically download the optimal ChromeDriver version on your cache and use it every time you need. Also the experimental part helps to avoid some error that might appear due to driver bugs

Related

selenium AttributeError: 'str' object has no attribute 'start'

im trying to make a program that works like this but i keep getting this error
Traceback (most recent call last):
File "/workspaces/vscode-remote-try-python/VirtualBrowser/main.py", line 7, in <module>
driver = webdriver.Chrome(service=r'VirtualBrowser/chromedriver')
File "/home/vscode/.local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
super().__init__(
File "/home/vscode/.local/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 101, in __init__
self.service.start()
AttributeError: 'str' object has no attribute 'start'
this is my code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromeOptions = webdriver.ChromeOptions()
chromeOptions.binary_location = "/workspaces/vscode-remote-try-python/VirtualBrowser/chromedriver"
chromeDriver = 'VirtualBrowser/chromedriver'
driver = webdriver.Chrome(service=chromeDriver)
driver.get("https://google.com")
input("Running...")
im not exactly the greatest at debugging and most of the code is fixes that ive tried and they have worked towards throwing less errors. any help would be appreciated, thanks
I think there is a problem in driver = webdriver.Chrome(service=chromeDriver) where service kwarg should be a service object.
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# ...
chrome_options = Options()
chrome_service = Service("path")
driver = webdriver.Chrome(options=chrome_options, service=chrome_service)

Simple Selenium Webdriver importing doesnt work

**from selenium import webdriver
browser = webdriver.chrome("./chromedriver.exe")
browser.get("https://www.google.com")
I wrote the above code in visual studio code; however, every time I do this, this error occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'browser' is not defined
I downloaded the Selenium library and web driver matches my Chrome, but I'm unsure what to do.
I Googled and YouTubed it for almost 3 hours and it does not work at all.
Please help me solve this problem.
Start learning Selenium from any of the online tutorials.
Always careful on case sensitive while coding through Selenium with Python:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
browser = webdriver.Chrome(service=Service("<path of the chromedriver.exe>"))
browser.get("https://www.google.com")

seleniumwire_options=None: SyntaxError: invalid syntax

I am using selenium-wire to click a button using Chrome browser. Up until today, my code worked fine. However, now I get the following error and I am not sure why:
Traceback (most recent call last):
File "Scraping_fx.py", line 1, in <module>
from seleniumwire import webdriver
File "C:\Users\me\Anaconda3\envs\project\lib\site-
packages\seleniumwire\webdriver\__init__.py", line 3, in <module>
from .browser import Chrome, Edge, Firefox, Safari # noqa
File "C:\Users\me\Anaconda3\envs\project\lib\site-
packages\seleniumwire\webdriver\browser.py", line 14
def __init__(self, *args, seleniumwire_options=None, **kwargs):
^
SyntaxError: invalid syntax
I am using python 2.7.16; selenium-wire 1.0.4.
from seleniumwire import webdriver
from selenium.common.exceptions import ElementClickInterceptedException,NoSuchElementException
import logging
def scrape_website(url):
# Configure browser driver
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
#Instantiate driver and navigate to URL
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(30)
driver.get(url)
Am I missing something here?
Can't see anything wrong with your code :)
But selenium-wire 1.0.4 requires Python 3.4+.
You can find all the info on pypi.org - here

Why am I getting NameError: name 'ActionChains' is not defined?

Im pretty new to python and trying to fill out a web form automated.
Im getting this Error:
Traceback (most recent call last):
File "main.py", line 24, in
ActionChains(browser)\
NameError: name 'ActionChains' is not defined
And this is my code:
from time import sleep
from selenium import webdriver
browser = webdriver.Chrome ('/Users/max/Downloads/chromedriver')
browser.get ('http://www.brix.de/computer/web_html_php_et_al/formular-test_smm_01.html')
inputs = browser.find_element_by_xpath(
'/html/body/form[1]/table')
ActionChains(browser)\
.move_to_element(input[vorname]).click()\
.send_keys('name')\
.move_to_element(input[name]).click()\
.send_keys('Surname')\
.perform()
Can somebody help me please?
I think you are missing the import, try the following:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome ('/Users/kiran/Downloads/chromedriver')
browser.get ('http://www.brix.de/computer/web_html_php_et_al/formular-test_smm_01.html')
inputs = browser.find_element_by_xpath(
'/html/body/form[1]/table')
ActionChains(browser)\
.move_to_element(input[name]).click()\
.send_keys('name')\
.move_to_element(input[vorname]).click()\
.send_keys('Surname')\
.perform()

Importing 'Keys' from 'selenium.webdriver.common.keys'

I am trying to execute the below code. I exclusively tried to import Keys from webdriver, but it still does not work.
from selenium import webdriver
import selenium.webdriver.common.keys
driver = webdriver.Firefox()
page = driver.get("https://www.python.org/")
print (driver.title)
finder = driver.find_element_by_class_name("search-field")
finder.send_keys("Python Test")
finder.send_keys(Keys.RETURN)
Output:
Welcome to Python.org
Traceback (most recent call last):
File "C:/Users/Arvind/Desktop/Python Tests/selenium_tests.py", line 9, in
<module>
finder.send_keys(Keys.RETURN)
NameError: name 'Keys' is not defined
>>>
You need to have
from selenium.webdriver.common.keys import Keys instead of
import selenium.webdriver.common.keys.
Then your code would run fine.

Categories