1

I want to create an Auto-Login for Instagram.com with Python3. I informed myself that I have to use this to work with websites:

from selenium import webdriver

But on ALL tutorials I found they didn't had any problems with this, but I got an error message:

unresolved import selenium Error Message

I browsed other questions and found this one that should solve my question too, but the Website and the Code they propose didn't solve my problem.

My full code so far:

from selenium import webdriver
from time import sleep

username = "username"

class InstaBot:
    def __init__(self):
        self.driver = webdriver.Chrome()
        driver.get("https://instagram.com")
        sleep(2)
        login_field =  driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input').send_keys(username)


InstaBot()

How can I fix this problem?

Edit: I now know that I have to install Selenium first with that code in command prompt:

pip3 install selenium

I did it and watched this Tutorial on YouTube and make it like him, but it just doesn't work. :(

It prints to console a very long Error message:

Traceback (most recent call last):
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37- 
32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37- 
32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht 
finden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 16, in <module>
InstaBot()
File "main.py", line 8, in __init__
self.driver = webdriver.Chrome()
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' 
executable needs to be in PATH. Please see 
https://sites.google.com/a/chromium.org/chromedriver/home
Aynos
  • 288
  • 1
  • 13

3 Answers3

2

Edit to answer since question is also edited:

Error message is clear that you need to install browser driver to use it with Selenium. Since you are using webdriver.Chrome(), you also need to install Chrome driver. https://sites.google.com/a/chromium.org/chromedriver/downloads https://www.selenium.dev/documentation/en/webdriver/driver_requirements/

Since chromedriver is just a zip file, you can just extract and configure environment variable(PATH) pointing to this extracted directory.

If you don't want to deal with PATH env variables, you can specify the chromedriver path in code like:

self.driver = webdriver.Chrome("/path/to/chromedriver")

Error message(unresolved import) is some linting/codecorrection problem with VSCode, becuae selenium module is alredy imported and script is failing at later parts while trying to start Chrome using chromedriver.

original answer:

From your problem it feels like you have another python file with name selenium.py in your current directory or name of your python script is selenium.py itself or your scripts are in the directory with name selenium.

Python checks in current directory first for imported module and it treats every other python file in current directory as module. So rename those files this will be solved.

Tejas Sarade
  • 1,144
  • 12
  • 17
  • The folder where the only file `main.py` is, is named: IGLogin – Aynos May 09 '20 at 07:28
  • If you open python console and run `from selenium import webdriver` do you get the same error message? – Tejas Sarade May 09 '20 at 07:53
  • That is correct, it means selenium module itself installed correctly and can be imported in Python without error. As you have added exact error you are getting, I have corrected my answer too. – Tejas Sarade May 09 '20 at 08:24
  • I downloaded Chromedriver but Windows Defender detected it as Risk for my PC??!! – Aynos May 09 '20 at 08:40
  • Since chromedriver can be used to run Chrome without user knowledge, it is possible for Windows Defender to flag it. I am on latest version of Windows 10 and I don't see any warning. Since it is downloaded from official google site, I don't see any reason to worry about that. – Tejas Sarade May 09 '20 at 08:47
  • Ok. But how to install it correctly? I visited the site you posted and did what they say but `chromedriver` don't make any output when I type it in CMD. – Aynos May 09 '20 at 08:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213471/discussion-between-tejas-sarade-and-jonas-x). – Tejas Sarade May 09 '20 at 09:06
1

Your IDE is unable to locate your Selenium package. When you run pip show selenium does it print any output? If so, look at the line beginning with Location:. Make sure that location is found in the output of import sys; print(sys.path).

Furthermore, I would suggest either simplifying your class into a single function if what is shown is all you will be doing, or if you plan on adding more logic in the future, moving the actual execution of the driver code to a method other than the constructor.

from selenium import webdriver
from time import sleep

class InstaBot:
    def __init__(self, username):
        self.driver = webdriver.Chrome()
        self.username = username

    def login(self):
        response = self.driver.get("https://instagram.com")
        sleep(2)
        response.find_element_by_xpath(
            "/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input"
            ).send_keys(self.username)


bot = InstaBot(username="jonasx_yt")
bot.login()
vulpxn
  • 731
  • 1
  • 6
  • 14
  • If I run the command `pip show selenium` in Terminal for a sec nothing happens and then it shows no output. It makes nothing – Aynos May 09 '20 at 07:27
1

You have to install it manually instead of using pip.

Step1: Download the package from the following link:

Download the package

Step2: Unpack it

- tar xf selenium-3.141.0.tar.gz

- cd selenium-3.141.0/

- ls

- python3 setup.py install

Now you're good to go.

Anshul Vyas
  • 633
  • 9
  • 19