SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.72k stars 8.19k forks source link

[🐛 Bug]: Issues with driver using python #12726

Closed gbengaorelusi closed 1 year ago

gbengaorelusi commented 1 year ago

What happened?

chrome

How can we reproduce the issue?

from selenium import webdriver
import os
import time
import datetime

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class InstagramBot:
    def __init__(self, username, password, hashtags):
        self.username = username
        self.password = password
        self.hashtags = hashtags

        self.driver = webdriver.Chrome('/chromedriver.exe')

        time.sleep(1)
        self.login()

        self.by = By
        self.ec = EC
        self.WebDriverWait = WebDriverWait

    def login(self):
        self.driver.get('https://www.instagram.com/accounts/login/')
        self.WebDriverWait(self.driver, 20).until(self.ec.presence_of_element_located((self.by.Name, 'username')))
        self.WebDriverWait(self.driver, 20).until(self.ec.presence_of_element_located((self.by.Name, 'password')))
        self.WebDriverWait(self.driver, 20).until(self.ec.element_to_be_clickable((self.by.XPATH, '//*[contains(text(), "Log In")]')))

        self.driver.find_element_by_name('username').send_keys(self.username)
        self.driver.find_element_by_name('password').send_keys(self.password)
        self.driver.find_element_by_xpath('//*[contains(text(), "Log In")]').click()

ig = InstagramBot("***", "***", "test")
ig.login()

this is my code, where did I go wrong

Relevant log output

C:\Users\pers101\AppData\Local\Programs\Python\Python311\python.exe "C:\Users\pers101\Desktop\scraping apis\scrapenv\Scripts\py insta\src\myscrapcom.py" 
Traceback (most recent call last):
  File "C:\Users\pers101\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\pers101\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 76, in driver_location
    browser = options.capabilities["browserName"]
              ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\pers101\Desktop\scraping apis\scrapenv\Scripts\py insta\src\myscrapcom.py", line 36, in <module>
    ig = InstagramBot("dtreker", "ayomideTREKER4#", "test")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\pers101\Desktop\scraping apis\scrapenv\Scripts\py insta\src\myscrapcom.py", line 17, in __init__
    self.driver = webdriver.Chrome('/chromedriver.exe')
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\pers101\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\Users\pers101\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\pers101\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
                                         ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'capabilities'

Process finished with exit code 1

Operating System

Windows 10

Selenium version

py Version: 4.12.0

What are the browser(s) and version(s) where you see this issue?

chrome

What are the browser driver(s) and version(s) where you see this issue?

chrome driver 116

Are you using Selenium Grid?

no

github-actions[bot] commented 1 year ago

@gbengaorelusi, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

titusfortner commented 1 year ago

This is no longer supported:

webdriver.Chrome('/chromedriver.exe')

You don't need to specify the driver at all any longer, Selenium will manage it for you. If you need to use a custom driver, you can specify it in a Service class: https://www.selenium.dev/documentation/webdriver/drivers/service/#driver-location

gbengaorelusi commented 1 year ago

`class InstagramBot: def init(self, username, password, hashtags): self.username = username self.password = password self.hashtags = hashtags

    self.driver = webdriver.Chrome('/chromedriver.exe')

    time.sleep(1)
    self.login()

    self.by = By
    self.ec = EC
    self.WebDriverWait = WebDriverWait

`

are you saying I should remove my self.driver variable, and also what imports should I remove from below

`from selenium import webdriver import os import time import datetime

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By`

titusfortner commented 1 year ago

You can see a complete working example here - https://www.selenium.dev/documentation/webdriver/getting_started/first_script/

github-actions[bot] commented 11 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.