使用selenium和webdriver(chrome)python OSX在Instagram中填写登录表单

我想使用selenium登录Instagram,但我似乎无法在字段中输入值.

这是我的脚本:

#go to this address
browser.get('https://www.instagram.com')

#sleep for 1 seconds
sleep(1)

#find the 'login' button on homepage
login_elem = browser.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')

#navigate to login page
login_elem.click()

从这里开始遇到麻烦:

#locate the username field within the form
unform = browser.find_element_by_xpath(
'//*[@id="f3b8e6724a27994"]')

#clear the field
textunform.clear()

#enter 'test' into field
unform.send_keys('test')
最佳答案
Instagram上的用户名字段是React Native元素,因此您必须引导WebDriverWait,然后调用send_keys()方法,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
browser.get('https://www.instagram.com')
login_elem = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_elem.click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("anon")

浏览器截图:

BrowserScreenshot

转载注明原文:使用selenium和webdriver(chrome)python OSX在Instagram中填写登录表单 - 代码日志