首页 > 解决方案 > 使用 Selenium 进行 WebScraping:单击并打开按钮

问题描述

我正在尝试使用 Python 和 Selenium 抓取该网站:https ://www.kayak.it/explore/MIL-anywhere/20210801,20210801

我想单击并打开每个旅行框(带有城市名称和航班价格的框,当您单击它时,您会获得更多详细信息)。

我不明白为什么我的代码不起作用,我没有收到任何错误,什么也没有。

为了处理 cookie,我使用了第一部分代码。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.maximize_window()
wd.implicitly_wait(30)
wd.get("https://www.kayak.it/explore/MIL-anywhere/20210810,20210810")
wait = WebDriverWait(wd, 20)
try:
    if(len(wd.find_elements(By.CSS_SELECTOR, "button[id$='accept']"))) > 0:
        print("click privacy")
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id$='accept']"))).click()
    else:
        print("button not find")
except:
    print("error")
    pass

import time
chrome_options = webdriver.ChromeOptions() 
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.get("https://www.kayak.it/explore/MIL-anywhere/20210810,20210810")
time.sleep(5)
if (len(wd.find_elements_by_id("_eY._iwG._ihs._irH")) > 0):
  print("Click travel box")
  wd.find_elements_by_id("_eY._iwG._ihs._irH")[0].click()

标签: pythonseleniumselenium-webdriverweb-scraping

解决方案


推荐阅读