# conda create -n python39 python=3.9# 导入 selenium 和 time 库
from webdriver_manager.chrome import ChromeDriverManager # python -m pip install webdriver-manager
# service = ChromeService(executable_path=ChromeDriverManager().install())
# driver = webdriver.Chrome(service=service)from selenium import webdriver # python -m pip install selenium
# driver = webdriver.Chrome(executable_path="chromedriver.exe") # 创建一个浏览器对象
# driver = webdriver.Chrome(executable_path="msedgedriver.exe") # 创建一个浏览器对象
from selenium.webdriver.chrome.service import Service# https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/?form=MA13LH#downloads
service = Service(executable_path="ChromePortable64-110.0.5481.178\chromedriver.exe")
# 连接到127.0.0.1:9222
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9515") # --remote-debugging-port=9222
#
driver = webdriver.Chrome(service=service, options=options) # 1. 使用驱动实例开启会话
print(driver)import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 10) # 设置显式等待为10秒
original_window = driver.current_window_handle # 存储原始窗口的 ID
print("original_window: ", driver.current_window_handle)
# assert len(driver.window_handles) == 1
# 循环执行,直到找到一个新的窗口句柄
# driver.quit()
for window_handle in driver.window_handles:print("window_handle: ", window_handle)# 关闭 window_handledriver.switch_to.window(window_handle)# print(driver.title)# driver.title 包含 "在线考试" if "baidu.com" in driver.current_url and "百度" in driver.title:# driver.close()breakelse:# driver.close()pass
# exit()
tingzhi = driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[2]")[0]
print(tingzhi.text)
print(tingzhi.get_attribute('class'))
# exit()
while True:anniu= driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[1]")[0]if(anniu.text == "按钮"):if(anniu.get_attribute('class') == ""):anniu.click()while True:#等待text变为xxanniu= driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[1]")[0]if(anniu.text == "按钮"):print("开始计时...")#运行29分钟time.sleep(28 * 60) # 29m# time.sleep(10) # 10sbreakelse:print("启动中...")time.sleep(3)continue tingzhi = driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[2]")[0]if(tingzhi.get_attribute('class') == ""):tingzhi.click()while True:#等待停止变蓝色anniu= driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[1]")[0]if(anniu.get_attribute('class') == ""):print("停止完成")breakelse:print("停止中...")time.sleep(3)continue continue
# driver.quit()
python -m pip install webdriver-manager selenium -y
用Python的好处就是稳定,完全模拟了手动操作,用浏览器控制台的JavaScript总感觉是假死啊。