使用selenium登录Instagram时,显示“密码错误”错误

问题描述 投票:0回答:1

我编写了以下代码来登录 Instagram,但收到错误消息,提示我输入了错误的密码。虽然这是正确的。一般来说,在驱动程序的 chrome 窗口中,我无法登录 insta,甚至无法通过脚本登录。

我使用的代码如下。我尝试了很多不同的隐身方式。但结果是一样的。当我从简单的登录窗口登录时,一切都很好。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium_stealth import stealth
import random

username = '***'
password = '***'

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)

stealth(driver,
        user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
        languages=["uk-UA", "uk"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

try:
    driver.get('https://www.instagram.com/')

    # Wait for the username input field to be clickable with an increased wait time
    username_input = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.NAME, 'username'))
    )

    username_input.clear()
    time.sleep(5)
    username_input.send_keys(username)

    # Wait for the password input field to be clickable
    password_input = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.NAME, 'password'))
    )

    password_input.clear()
    time.sleep(4)
    password_input.send_keys(password)
    time.sleep(1)
    password_input.send_keys(Keys.ENTER)

#     Add a wait for some element on the next page if needed
#     WebDriverWait(driver, 20).until(
#         EC.presence_of_element_located((By.XPATH, 'your_xpath_here'))
#     )

except NoSuchElementException as e:
    print(f"Element not found: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Ensure that the entire browser window is closed
    driver.quit()
selenium-webdriver web-scraping selenium-chromedriver instagram
1个回答
0
投票

我已经测试了你的代码,它非常适合我。请仔细检查您在

[send_keys]
方法中输入的用户名和密码是否正确。我附上我的自动化结果screenshot2供您参考。”

© www.soinside.com 2019 - 2024. All rights reserved.