Reputation: 187
I'm pretty new to Python and Selenium. I was following a tutorial on how to code an Instagram bot to gain followers and likes.
The program opens a Firefox window as it should, but then when it has to fill in the username and password field it does nothing. Then when i close down the window i find myself with this error.
It's really odd that i get this error because i reviewed the elements on https://www.instagram.com/accounts/login/ and in the username field there was a name='username' as well as an name='password' in the password field.
Can anybody help me out?
This is a piece of the code:
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Firefox()
self.login()
def login(self):
self.driver.get('https://www.instagram.com/accounts/login/')
self.driver.find_element_by_name('username').send_keys(self.username)
self.driver.find_element_by_name('password').send_keys(self.password)
This is the error message.
Traceback (most recent call last):
File "C:/Users/Python Programming/PycharmProjects/insta_bot/bot.py", line 25, in <module>
ig_bot = InstagramBot('temp_username', 'temp_password')
File "C:/Users/Python Programming/PycharmProjects/insta_bot/bot.py", line 14, in __init__
self.login()
File "C:/Users/Python Programming/PycharmProjects/insta_bot/bot.py", line 19, in login
self.driver.find_element_by_name('username').send_keys(self.username)
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Python Programming\PycharmProjects\insta_bot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="username"]
Upvotes: 0
Views: 1633
Reputation: 1991
It appears that you may need to wait for some scripts on the page to finish before that element exists.
here are the import statements you'll need:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Use this code to wait for that element before your send_keys
line:
WebDriverWait(self.driver,10).until(
EC.presence_of_element_located((By.NAME, "username")))
Upvotes: 4