当前位置:网站首页>Selenium basic knowledge automatically login Baidu Post Bar

Selenium basic knowledge automatically login Baidu Post Bar

2022-07-24 07:36:00 everyone_ yi

"""  Dialog processing   Floating pop-up dialog   Such as login box or other functions iframe  Baidu post bar : 1. single click " Sign in " link , The login box will pop up  2. Click... In the login box " User name login " Options , Show the real login interface  3. Fill in account and password , single click " Sign in " Button  """

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

data={
    
    'url':'https://tieba.baidu.com/index.html',
    'first_btn_classname':'u_menu_item',
    'show_account_login_id':'TANGRAM__PSP_4__footerULoginBtn',
    'usename_id':'TANGRAM__PSP_4__userName',
    'password_id':'TANGRAM__PSP_4__password',
    'login_btn_id':'TANGRAM__PSP_4__submit',
    'username':'*********',
    'password':'**********![ Please add a picture description ](https://img-blog.csdnimg.cn/51f8e1a67d1744e8abc99fb963c569c0.png)
'
}


driver = webdriver.Edge()
driver.get(data['url'])

#  Click on the login link 
# first_btn = driver.find_element_by_css_selector("#com_userbar > ul > li.u_login > div > a")
first_btn = driver.find_element(by=By.CLASS_NAME,value=data['first_btn_classname'])
first_btn.click()
time.sleep(2)

#  Click on 【 User name login 】 Options 
show_account_login = driver.find_element(by=By.ID,value=data['show_account_login_id'])
show_account_login.click()
time.sleep(2)

#  Fill in account and password 
# TANGRAM__PSP_10__userName
# TANGRAM__PSP_10__password

driver.find_element(by=By.ID,value=data['usename_id']).send_keys(data['username'])
time.sleep(2)
driver.find_element(by=By.ID,value=data['password_id']).send_keys(data['password'])
time.sleep(2)

driver.find_element(by=By.ID,value=data['login_btn_id']).click()

There is only one problem, which is the first step Click on the login link Use classname When positioning Because there can be no spaces So it will match the first one In this page The first one matched will be other buttons Will go wrong

This button on Baidu homepage class by u_menu_item u_menu_bdhome j_u_menu_bd_home_link

In the code, it is written in front of the login button So I will match Baidu's button first
change to the use of sth. u_login To match Or use elements Search and select the second result is also ok
 Please add a picture description
 Please add a picture description

Just change it

data={
    
    'url':'https://tieba.baidu.com/index.html',
    'first_btn_classname':'u_login',
    'show_account_login_id':'TANGRAM__PSP_4__footerULoginBtn',
    'usename_id':'TANGRAM__PSP_4__userName',
    'password_id':'TANGRAM__PSP_4__password',
    'login_btn_id':'TANGRAM__PSP_4__submit',
    'username':'*****',
    'password':'*****'
}

There will also be the problem of SMS verification code behind this It will be solved later

原网站

版权声明
本文为[everyone_ yi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221756317625.html