gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:26:53 26 4
gpt4 key购买 nike

我想使用 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')

最佳答案

这里有一个技巧,除了搜索按钮(登录)之外,还有一种更好的方法可以在没有按钮的情况下登录。如何?让我们看看:

导入您需要的包:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
#Select the driver, In our case we will use Chrome.
chromedriver_path = 'chromedriver.exe' # Change this to your own chromedriver path!
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)
username = webdriver.find_element_by_name('username')
username.send_keys('yourUsername')
password = webdriver.find_element_by_name('password')
password.send_keys('yourPassword')
#instead of searching for the Button (Log In) you can simply press enter when you already selected the password or the username input element.
submit = webdriver.find_element_by_tag_name('form')
submit.submit()

您可以复制代码并直接运行(即使没有真实的用户名或密码)从 ChromeDriver 获取 webdriver (chromedriver.exe)

关于python - 使用 selenium 和 webdriver (chrome) 在 Instagram 中填写登录表单 python OSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49938766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com