练习场景:百度登录时,勾选下次自动登录按钮;
一、简单版
具体代码如下:
# coding=utf-8 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(8) driver.get(‘https://www.baidu.com/‘) driver.implicitly_wait(8) driver.find_element_by_xpath("//*[@id=‘u1‘]/a[8]").click() time.sleep(1) driver.find_element_by_xpath("//*[@title=‘用户名登录‘]").click() time.sleep(1) driver.find_element_by_xpath("//*[@name=‘memberPass‘]").click() time.sleep(1) driver.find_element_by_xpath("//*[@name=‘memberPass‘]").click()
这个单个复选框,如果有多个,可以用for循环来做(这里用单选框radio举例)
for i in driver.find_elements_by_xpath("//*/input[@type=‘radio‘]"): i.click()
二、整合版
具体代码:
# coding=utf-8 from selenium import webdriver import time driver = webdriver.Chrome() url=‘https://baidu.com‘ def press_login(): login = driver.find_element_by_xpath("//*[@id=‘u1‘]/a[8]") login.click() def press_login_by_account(): LoginByAccount = driver.find_element_by_xpath("//*[@title=‘用户名登录‘]") LoginByAccount.click() def press_check_box(): driver.find_element_by_xpath("//*[@name=‘memberPass‘]").click() driver.get(url) print(‘登录网址:‘,url) time.sleep(2) press_login() print(‘成功点击【登录】按钮‘) time.sleep(2) press_login_by_account() print(‘成功点击【用户名登录】‘) time.sleep(2) press_check_box() print(‘点击【下次自动登录】‘) time.sleep(2) press_check_box() print(‘再次点击【下次自动登录】‘) print(‘脚本完成。‘)
参考文章:https://blog.csdn.net/u011541946/article/details/69680990
原文地址:https://www.cnblogs.com/zhaocbbb/p/12638265.html
时间: 2024-11-07 21:16:42