Files
xkwsoftlist/test_selenium_single.py
2026-02-09 08:59:54 +08:00

62 lines
2.3 KiB
Python

import os
import re
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
def get_driver():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
return driver
def test_single_fetch(url):
print(f"Testing Selenium with URL: {url}")
driver = None
try:
driver = get_driver()
driver.get(url)
print("Waiting for page elements...")
wait = WebDriverWait(driver, 15) # 给足够的时间处理验证
try:
# 尝试定位 other-info 节点
other_info = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "other-info")))
print("Successfully found 'other-info' node!")
# 提取信息
time_node = other_info.find_element(By.CLASS_NAME, "time")
views_node = other_info.find_element(By.CLASS_NAME, "views")
download_node = other_info.find_element(By.CLASS_NAME, "download")
print(f"Date Result: {time_node.text}")
print(f"Views: {views_node.text}")
print(f"Downloads: {download_node.text}")
return True
except Exception as e:
print(f"Failed to find elements: {e}")
# 打印当前页面标题,看看是不是还在挑战页面
print(f"Current Page Title: {driver.title}")
return False
finally:
if driver:
driver.quit()
if __name__ == "__main__":
test_url = "https://www.zxxk.com/soft/38837976.html"
test_single_fetch(test_url)