简体   繁体   English

在 Python 中使用 Selenium 将登录信息输入到 Instagram

[英]Using Selenium in Python to enter Login Information to Instagram

Does anybody know how to automate the login to Instagram using python.有谁知道如何使用 python 自动登录 Instagram。 I am able to login in other sites, but Instagram just won't work.我可以登录其他网站,但 Instagram 无法正常工作。

I used this code:我使用了这个代码:

element = driver.find_element_by_xpath('//input[@name='username']')
element.send_keys('my username')

Where is my error?我的错误在哪里?

The username field is contained in an <iframe> .用户名字段包含在<iframe> Selenium can only access objects in the current frame. Selenium 只能访问当前帧中的对象。 You'll need to use switch_to.frame() in order to access the content you want to interact with:您需要使用switch_to.frame()才能访问要与之交互的内容:

frame = driver.find_element_by_css_selector("iframe.hiFrame")
driver.switch_to.frame(frame)
element = driver.find_element_by_xpath("//input[@name='username']")

When you're finished in the <iframe> , you'll need to switch back out, using switch_to.default_content() :<iframe>完成后,您需要使用switch_to.default_content()切换回来:

driver.switch_to.default_content()

You can try this code, hope this works !你可以试试这个代码,希望它有效!

from selenium import webdriver

username = "Your Username"
password = "Your Password"

getdriver = ("https://www.instagram.com/accounts/login/")

driver = webdriver.Firefox()
driver.get(getdriver)

driver.find_element_by_xpath("//input[@name='username']").send_keys(username)
driver.find_element_by_xpath("//input[@name='password']").send_keys(password)
driver.find_element_by_xpath("//button[contains(.,'Log in')]").click()

Try adding a time.sleep(timeOutInSeconds) after getting the driver and before you query for elements, example:尝试在获取驱动程序之后和查询元素之前添加 time.sleep(timeOutInSeconds),例如:

import time

....
....

driver.get(URL)

time.sleep(3)

element = driver.find_element_by_xpath("//input[@name='username']")
element.send_keys('my username')

It could be that your internet connection is slow and the login form is not loaded, immediately after the driver opens the website.可能是您的互联网连接速度较慢,并且在驱动程序打开网站后未立即加载登录表单。

Put the following code login to instagram using selenium把下面的代码用selenium登录instagram

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
drv= webdriver.Chrome()
drv.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
drv.find_element_by_name("username").send_keys('your_username_here')
drv.find_element_by_name("password").send_keys('your_password_here') 
drv.find_element_by_name("password").send_keys(u'\ue007')

注意你的语法。

element = driver.find_element_by_xpath("//input[@name='username']")
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

username = driver.find_element_by_xpath("//input[@name='username']")
password = driver.find_element_by_name("password")

username.send_keys("YOUR USERNAME")
password.send_keys("YOUR PASSWORD")

password.send_keys(Keys.RETURN)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM