简体   繁体   English

使用 selenium 和 python 登录后,我无法导航到 Instagram 页面

[英]I can't navigate to instagram pages after login using selenium and python

I'm making an instagram bot.我正在制作一个 Instagram 机器人。 def nav_user should navigate me to a user page even after log in has failed due to a wrong username and password.即使由于错误的用户名和密码登录失败, def nav_user也应该将我导航到用户页面。

It should give me a link like this: https://www.instagram.com/therock/它应该给我一个这样的链接: https://www.instagram.com/therock/
But actually, I am getting this link: https://www.instagram.com/accounts/login/?next=/therock/但实际上,我得到了这个链接: https://www.instagram.com/accounts/login/?next=/therock/

Any suggestions?有什么建议么?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os 

class instagrambot:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome('./chromedriver')
        self.login()
        
    def login(self):
        self.driver.get('https://instagram.com/accounts/login')
        sleep(2) 
        self.driver.find_element_by_name('username').send_keys(self.username)
        password_field = self.driver.find_element_by_name('password')
        password_field.send_keys(self.password)
        self.driver.find_element_by_name('password').send_keys(Keys.RETURN)
    
    def nav_user(self, user):
        sleep(3)
        self.driver.get('https://instagram.com/' + user)
 
bot = instagrambot('username123', 'password')
bot.nav_user('therock')

I think you should wait for a while for page loaded after logged in and navigate to new user, you can use wait page load function like this:我认为您应该在登录后等待一段时间加载页面并导航到新用户,您可以像这样使用等待页面加载 function:

browser = webdriver.Chrome()
browser.get("Your_URL")
time = 5
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'ELEMENT_ID')))
    print "LOAD DONE"
except TimeoutException:
    print "LONG TIME RESPONSE"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中使用Selenium登录后保持会话? - How can I maintain session after login using Selenium in Python? 在 Python 中使用 Selenium 将登录信息输入到 Instagram - Using Selenium in Python to enter Login Information to Instagram 无法使用 selenium 和 python 获取 instagram 搜索框 - Can't get instagram search box using selenium and python 我如何使用 selenium 在 python 中打印 instagram 回复消息 - how can i print instagram reply message in python using selenium 如何使用 selenium 保存密码(登录信息),所以我不必在 Instagram 上一次又一次地登录 - how can I save password(login info) using selenium , so I have to not login again and again on Instagram 我如何使用硒python浏览分页? - How can i navigate through pagination using selenium python? 如何使用 python selenium 登录? - How can I login using python selenium? Instagram通过Selenium for Python登录 - Instagram login through Selenium for Python 在 Instagram 更改其 API 流程后,如何使用 Python 使用 Selenium 刮取 Instagram? 我无法找到所有条目,只能找到 12 个 - How to scrape Instagram using Python using Selenium after Instagram changed their API process? I'm unable to find all the entries, can only find 12 我无法通过请求登录 Instagram - I can't login to Instagram with Requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM