Simple Selenium Webdriver importing doesnt work - python

**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")

Related

Selenium: AttributeError: 'Options' object has no attribute 'set_headless'

I'm wanting to write a python script using Selenium to scrape a website. Following along with the Real Python article on it, I literally copy and pasted the following code into a py file:
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless # Operating in headless mode
browser = Firefox(options=opts)
browser.get('https://duckduckgo.com')
Running the script I get the following error:
opts.set_headless()
AttributeError: 'Options' object has no attribute 'set_headless'
Attempted to follow this article and commented out the opts.set_headless() attribute and added opts.headless = True but now I get the following error:
Traceback (most recent call last):
File "/home/usr/local/folder/scraper.py", line 10, in <module>
browser = Firefox(options=opts)
File "/home/usr/local/folder/scraper/venv/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 192, in __init__
self.service.start()
File "/home/usr/local/folder/scraper/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 106, in start
self.assert_process_still_running()
File "/home/usr/local/folder/scraper/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 119, in assert_process_still_running
raise WebDriverException(f"Service {self.path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: -6
I verified that the geckodriver is located in my $PATH so I have no idea why none of this isn't working. I am using selenium v4.7.2.
After much hair pulling, I was able to determine that almost all articles on the internet dealing with Selenium use deprecated methods and attributes. Hopefully this answer will help many others who have been trying to use this library.
First, the .set_headless() method is fully deprecated and doesn't work. The Python Forums had a helpful discussion around it. In order to use a headless browser, you need to use .add_argument("--headless") and not any other way.
Second, there is now the Service() class that needs to be imported and used for any executable_path= pointing to the geckodriver and any other paths such as logs. These two posts helped on this matter: stackoverflow_1 and stackoverflow_2.
Third, after fixing the code and using the correct modules, attributes, methods and arguments, it was still getting hung up. Searching the logs was pointing to a socket timeout and an issue that was being dealt with the dev team in Sep 2022. This helped me realize that the geckodriver version linked in the original Real Python article I was using was long outdated and needed to be updated to the latest version, which is v0.32.0 at the time of writing.
However, that wasn't why it was getting hung up. I decided to comment out the headless argument and that showed that the Firefox browser was the issue. Apparently, with ubuntu 22.04, Firefox is installed by default with snap and needs to be installed as a .deb file. Here is a good article explaining it.
So ultimately, many different issues with this library and it's constantly being updated with past features, which most articles on the internet use, are all deprecated. The Selenium documentation isn't the greatest either. Here is my final code with the previous issues commented out:
# from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
# Setup--
options = Options()
options.add_argument("--headless")
service = Service(executable_path="/home/$PATH/location/geckodriver.exe", log_path="/home/file/location/log/geckodriver.log")
# caps = webdriver.DesiredCapabilities().FIREFOX
# caps["marionette"] = True
### Deprecated
browser = Firefox(service=service, options=options)
# browser = webdriver.Firefox(firefox_profile=options, capabilities=caps, executable_path="~/bin/geckodriver.exe")
# Parse--
browser.get('https://duckduckgo.com')
logo = browser.find_element(by=id, value='logo_homepage_link')
print(logo[0].text)
browser.quit()
This should work:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.headless = True
browser = Firefox(options=opts, executable_path='C:\TheActualPathToThe\geckodriver.exe')
browser.get('https://duckduckgo.com')

Cant get this code using selenium and chromedriver to work in pycahrm

Been working on this for a while so help would be appreciated.
The code I'm on rn is:
from selenium import webdriver
PATH = "C:\\Program Files (x86)\\Chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.youtube.com/")
error code:
C:\Users\noahr\.virtualenvs\pythonProject-ME1SVWk8\Scripts\python.exe C:/Users/noahr/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "C:\Users\noahr\PycharmProjects\pythonProject\main.py", line 3, in <module>
driver = webdriver.chrome()
TypeError: 'module' object is not callable
Process finished with exit code 1
Looking at your code there seems to be no issue which was confirmed when I was able to execute your code on my machine (except for changing the chromedriver path).
However when I look at your logs, it mentions driver = webdriver.chrome(). If this is exactly what you have in your code, then it should be driver = webdriver.Chrome(PATH) - please note the capital "C" for Chrome as well the Path parameter.

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

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

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()

driver.get on selenium giving error

I have tried running the following command after I've imported everything I need (selenium, webdriver, keys):
>>> driver.get('https://steemit.com/')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'selenium.webdriver.chrome' has no attribute 'get'
I can't find any solution.
You are trying to call .get() method on a module. Instead, you need to instantiate a webdriver:
In [1]: from selenium import webdriver
In [2]: driver = webdriver.Chrome()
In [3]: driver.get('https://steemit.com/')
In [4]: print(driver.title)
Trending posts — Steemit
To avoid further selenium module usage confusion, please go through the "Get Started" section of the Python/Selenium documentation.
from your comment it looks like your code is:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.chrome # should be webdriver.chrome()
driver.get('')
and your traceback is:
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'selenium.webdriver.chrome' has no attribute 'get'
which you are missing the instantiation of the driver, also as pointed out below chrome should be capitalised, so this:
driver = webdriver.chrome
needs to become:
driver = webdriver.Chrome() # the brackets mean the object is created or 'instantiated'

Categories