文章

使用Selenium Web自动登录

selenium(Python浏览器自动化测试库)

官方教程

定位元素(通过XPath定位)

XPath工具:ChroPath(浏览器插件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import datetime


def log():
    driver = webdriver.Edge()
    driver.get("https:www.baiodu.com/")
    time.sleep(20)
    #driver.switch_to.frame(0)  #切换到ifname;索引,适用于只有一个iframe的情况
    user = driver.find_element(By.XPATH,"//body/div[1]/div[1]/div[1]/div[3]/div[3]/span[3]/div[1]/form[1]/div[2]/div[1]/div[1]/div[1]/input[1]") #定位到元素
    #user.clear() #清除默认值
    user.send_keys("008618223705397") #输入字符文本
    password = driver.find_element(By.NAME, "password") 
    password.send_keys("123")
    login = driver.find_element(By.ID, "dologin")
    login.click()  #模拟点击
    time.sleep(100)

while True:
    current_time = datetime.datetime.now()  #获取当前时间
    formatted_time = current_time.strftime("%H:%M:%S")
    print("当前时间(时:分:秒):", formatted_time)

    # 检查当前时间是否等于目标时间
    if formatted_time == "13:28:50":
        log()
        print("xixi")

    # 如果当前时间不匹配,等待一秒后继续检查
    time.sleep(1)


selenium如何实现iframe窗口的切换

本文由作者按照 CC BY 4.0 进行授权