尝试使用 python 在 instagram 上注册:https:/instagram.com/accounts/em



我遇到了麻烦,因为我尝试过的所有方法都不起作用。我尝试使用python请求。不幸的是,这不起作用,因为表单是在网页发布后 1 秒左右加载的,因此无法进行发布请求。不情愿地,我改用硒。但是,我无法按 xpath、id 或类定位元素,因为它们都是在每次网页加载时动态和随机加载的。还有其他方法可以注册吗?

请求:尝试传递发布请求时。它失败,因为页面/表单尚未加载(请参阅标题中的 URL)

m = {}
m["fullName"] = "test"
requests.post(url, data=m)

硒尝试:不起作用,因为每次 id/类名/等都是随机的。

email_field =  driver.find_element_by_id("f2e3acfde5540d")
name_field = driver.find_element_by_id("f15a1d5523914b")
username_field = driver.find_element_by_id("ff9c874585158")
password_field = driver.find_element_by_id("f233cff115218c8")

这是一个示例,如何使用xpath选择器通过Selenium登录/注册Instagram。我用硒包装器Elementium写了这个,以减少代码。但是,您可以对代码使用相同的选择器。

from elementium.drivers.se import SeElements
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.instagram.com/accounts/login/?hl=en')
se = SeElements(browser)
se.xpath('//input[@name="username"]', wait=True, ttl=2).clear().write('username')
se.xpath('//input[@type="password"]', wait=True, ttl=2).clear().write('password')
se.xpath('//button[contains(text(), "Log in")]', wait=True, ttl=2).click()
assert se.xpath('//a[text()="Profile"]', wait=True, ttl=2), 'User was not logged in.'

注册也是如此:

browser.get('https://www.instagram.com/?hl=en')
se = SeElements(browser)
se.xpath('//input[@name="emailOrPhone"]', wait=True, ttl=2).clear().write('366656')
se.xpath('//input[@name="fullName"]', wait=True, ttl=2).clear().write('Alex')
se.xpath('//input[@name="username"]', wait=True, ttl=2).clear().write('Alex')
se.xpath('//input[@name="password"]', wait=True, ttl=2).clear().write('password')
se.xpath('//button[contains(text(), "Sign up")]', wait=True, ttl=2).click()

最新更新