使用 selenium 和 webdriver (chrome) python OSX 在 Instagram 中填写登录表单 [英] Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX

查看:22
本文介绍了使用 selenium 和 webdriver (chrome) python OSX 在 Instagram 中填写登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to log in to instagram using selenium, but I can't seem to enter values into the fields.

这是我的脚本:

#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')

推荐答案

这有一个技巧,不是搜索按钮(登录),而是有一种更好的登录方式,无需它.如何?让我们看看:

There is a trick in this, instead of searching for the Button (Log In) there is a better way to log in without it. how? let's see:

导入你需要的包:

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

You can copy the code and run it directly (even without a real username or password) To get the webdriver (chromedriver.exe) from ChromeDriver

这篇关于使用 selenium 和 webdriver (chrome) python OSX 在 Instagram 中填写登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆