Selenium网站:点击并打开按钮

2024-05-17 22:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python和Selenium来创建这个网站:https://www.kayak.it/explore/MIL-anywhere/20210801,20210801

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

我不明白为什么我的代码不工作,我没有得到任何错误,什么都没有

为了处理cookies,我使用了代码的第一部分

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()

Tags: httpsimportidwwwitfindchromeexplore