Home > database >  What's stopping me from logging in on Instagram?
What's stopping me from logging in on Instagram?

Time:10-28

I've made a bot that automatically logs me to my Instagram account. Everything works fine, except the fact that I can't automatically press the "Log In" button. This is the error I get:

Traceback (most recent call last):
  File "C:/Users/Carole/PycharmProjects/InstagramBot1/main.py", line 40, in <module>
    LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword).send_keys(Keys.ENTER)
AttributeError: 'NoneType' object has no attribute 'send_keys'

Process finished with exit code 1

And here's my code:

# Importing

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.support import ui

# Setting everything up

service = Service("\Program Files (x86)\chromedriver.exe")


# Maximizing Window


# Program Starts

RandomnessTime = input("Select Randomness time:")
InstagramUsername = input("Enter Username:")
InstagramPassword = input("Enter Password:")
#WaitRandomnessTime = "wait" str(RandomnessTime)

driver = webdriver.Chrome(service=service)
driver.get('https://www.instagram.com/')
wait = ui.WebDriverWait(driver, 10)
driver.maximize_window()
wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.bIiDR"))).click()
RandomnessTime = WebDriverWait(driver, (1 - int(RandomnessTime)))

# Logging in

# def Login(self, )

LoginUsername = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "username"))).send_keys(InstagramUsername)
LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword).send_keys(Keys.ENTER)

# Clicking "Not Now".
ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()

Can someone spot the problem with what I've written?

CodePudding user response:

send_keys() is a method and returns nothing, to approach what you want you need to:

LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password")))
LoginPassword.send_keys(InstagramPassword)
LoginPassword.send_keys(Keys.ENTER)

CodePudding user response:

I would rather have this xpath

//div[text()='Log In']//parent::button

to click on Login button.

In you code something like this :

ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, "//div[text()='Log In']//parent::button"))).click()

The issue that you've with your code is :

ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword).send_keys(Keys.ENTER) 

does not return anything, not sure why you have a returned variable for that.

If you wanna stick with your way, I would suggest to pass enter in same send_keys like this.

ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword, Keys.ENTER)
  • Related