我希望使用Python Mechanize自动登录Instagram login,但我收到错误"No form matching nr=0"
,这是我的代码。
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
response = br.open("http://www.instagram.com/accounts/login)
br.select_form(nr = 0)
br.form["username"] = "[my username]"
br.form["password"] = "[my password]"
br.method = "POST"
response = br.submit()
有谁知道如何解决这个问题?表单没有名称,因此我无法手动输入名称。
答案 0 :(得分:0)
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/accounts/login/")
username = driver.find_element_by_xpath(
'//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[1]/input')
username.send_keys('my_username')
password = driver.find_element_by_xpath(
'//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/input')
password.send_keys('my_password')
password.submit()
input('logged in...?') # temporary so browser doesn't close immediately
driver.close()