python web browser selenium2
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.service import Service
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
import xlwt
import os
# Chemin vers geckodriver
driver_path = r'C:\Users\lcwuw\geckodriver-v0.34.0-win64\geckodriver.exe'
# Configurer le service avec le chemin vers geckodriver
service = Service(executable_path=driver_path)
# Initialiser le pilote de Firefox
driver = webdriver.Firefox(service=service)
driver.execute_script("window.open('');")
# 获取Cookie信息
cookies = driver.get_cookies()
for cookie in cookies:
print(cookie)
# Maintenir le navigateur ouvert pendant un certain temps
time.sleep(10)
# Ouvrir Google
driver.get('https://www.google.com')
try:
cookie_popup = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "L2AGLb")))
cookie_popup.click()
except Exception as e:
print("Bouton cookie non trouvé:", e)
# 等待cookie弹出框的出现
#cookie_popup = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.ID, "cookie-popup"))
#)
# 点击“接受”按钮
#accept_button = cookie_popup.find_element(By.ID, "accept-button")
#accept_button.click()
# Trouver la barre de recherche
#search_box = driver.find_element_by_name('q')
# Accepter les cookies en cliquant sur le bouton
try:
accept_button = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "accept-button")))
accept_button.click()
except Exception as e:
print("Bouton d'acceptation des cookies non trouvé:", e)
# Trouver la barre de recherche
search_box = driver.find_element(By.NAME, 'q')
# Entrer le terme de recherche et soumettre
search_box.send_keys('ul00.com')
search_box.send_keys(Keys.RETURN)
# Attendre quelques secondes pour que les résultats se chargent
# 等待新的结果加载
# 等待新的结果加载
# 初始化滚动次数计数器
scroll_count = 0
while scroll_count < 10: # 最多滚动10次
time.sleep(10) # 等待5秒钟
result_links = driver.find_elements(By.XPATH, '//div[@class="tF2Cxc"]/descendant::a')
found_ul00 = False
for link in result_links:
if "https://ul00.com" in link.text: # 检查链接的文本内容
link.click()
found_ul00 = True
break
if found_ul00:
break # 找到了匹配副标题的链接,停止循环
# 如果在第一页的搜索结果中没有找到匹配副标题的链接,则滚动到页面底部,继续寻找更多结果
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
scroll_count += 1 # 更新滚动次数计数器
# 如果还是没有找到 "ul00.com",可以添加适当的处理代码
time.sleep(15)
# Ouvrir un nouveau onglet et aller sur ul00.com
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get('http://ul00.com')
# 切换到新打开的窗口
#driver.switch_to.window(driver.window_handles[-1])
# 打开网页
#driver.get("网页URL")
# 等待Cookie弹窗出现
# Si le site ul00.com a une boîte de dialogue de cookies, cliquez sur le bouton "Accepter tout"
try:
accept_cookie_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//button[@aria-label="Autoriser"]')))
accept_cookie_button.click()
print("Bouton d'acceptation des cookies trouvé et cliqué avec succès.")
except Exception as e:
print("Bouton d'acceptation des cookies non trouvé ou erreur lors du clic :", e)
# 点击“接受所有Cookie”按钮
try:
accept_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//button[@aria-label="Autoriser"]')))
accept_button.click()
except Exception as e:
print("Bouton d'acceptation des cookies non trouvé:", e)
# Fermer le navigateur
driver.quit()