是什么阻止我登录Instagram?



我做了一个自动登录我的Instagram账户的机器人。一切都很好,除了我不能自动按下"登录"键。按钮。这是我得到的错误:

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

下面是我的代码:

# 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()
有人能指出我写的问题吗?

我宁愿使用这个xpath

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

点击登录按钮。

在你的代码中像这样:

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

你的代码的问题是:

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

不返回任何东西,不知道为什么要为此返回一个变量。

如果你想坚持自己的方式,我建议像这样在相同的send_keys中传递enter。

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

send_keys()是一个方法,不返回任何东西,要达到您想要的效果,您需要:

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

相关内容

  • 没有找到相关文章

最新更新