📜  使用Python Selenium登录Twitter

📅  最后修改于: 2021-04-16 06:16:31             🧑  作者: Mango

项目介绍:-
在这里,我们将研究一个简单的如何使用selenium登录twitter的方法。 Selenium是一个免费工具,可以跨特殊的浏览器自动进行试用。

要求:

  • Selenium – Selenium Python简介和安装

步骤如下:

  • 首先,使用此链接转到Twitter网站。
  • 然后通过紧急ctrl + shift + i单击调查元素,或进入浏览器设置并手动单击调查详细信息。
  • 然后在填写电话,电子邮件或用户名的框中导航然后复制x_path。

  • 然后浏览密码,然后复制x_path。

  • 然后导航“登录”按钮,然后复制x_path。

下面是实现:

可以使用用户名和密码替换要成功登录的帐户。此处的代码仅用于演示。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
  
# create instance of Chrome webdriver
driver = webdriver.Chrome() 
driver.get("https://twitter.com/login")
  
# find the element where we have to 
# enter the xpath
# fill the number or mail
driver.find_element_by_xpath('//*[@id ="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input').send_keys('XXXXXX0418')
  
# find the element where we have to 
# enter the xpath
# fill the password
driver.find_element_by_xpath('//*[@id ="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input').send_keys('PrXXXXXXXXX9')
  
# find the element log in 
# request using xpath 
# clicking on that element 
driver.find_element_by_xpath('//*[@id ="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[3]/div/div').click()

输出 –
您将在浏览器中selenium自动打开的情况下登录Twitter。