本文介绍: 用Python的好处就是稳定,完全模拟了手动操作,用浏览器控制台的JavaScript总感觉是假死啊。
# 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.178chromedriver.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 EC
wait = 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_handle
driver.switch_to.window(window_handle)
# print(driver.title)
# driver.title 包含 "在线考试"
if "baidu.com" in driver.current_url and "百度" in driver.title:
# driver.close()
break
else:
# 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变为xx
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 == "按钮"):
print("开始计时...")
#运行29分钟
time.sleep(28 * 60) # 29m
# time.sleep(10) # 10s
break
else:
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("停止完成")
break
else:
print("停止中...")
time.sleep(3)
continue
continue
# driver.quit()
python -m pip install webdriver-manager selenium -y
用Python的好处就是稳定,完全模拟了手动操作,用浏览器控制台的JavaScript总感觉是假死啊。
原文地址:https://blog.csdn.net/dzxxbj/article/details/135957628
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_66663.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。