python - 使用 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()
解决方案
推荐阅读
- swift - 通过枚举访问符合协议的静态结构
- amazon-web-services - Redshift - 跨账户集群同步
- html - 为什么我的 css 浮动没有达到我的预期?
- r - 闪亮的 numericInputs 总和未按预期更新
- openlayers - 如何每隔几分钟更改自定义标记图像(OpenLayers)?
- flutter - 根据最小尺寸包装小部件,然后拉伸它们以填充可用宽度
- sql - 为什么我收到错误“无效的对象名称 #temp3”
- unity3d - 如何使这些 Mocap Animation FBX 与 Unity 配合使用?
- laravel - Dusk 没有加载正确的 .env
- javascript - I can't reference an image in Next.js