python - Selenium 在打印结果之前不会等待单击按钮
问题描述
在点击显示完整航班列表的按钮后,我正在尝试使用 BeautifulSoup 从 Google Flights 中抓取结果。我添加了一个明确的等待,但由于某种原因这不起作用。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import pandas as pd
driver = webdriver.Chrome(executable_path="/Users/Shiva/Downloads/chromedriver")
driver.get('https://www.google.com/flights?hl=en#flt=/m/03v_5.IAD.2019-02-10*IAD./m/03v_5.2019-02-11;c:USD;e:1;sd:1;t:f')
xpath = '//*[@id="flt-app"]/div[2]/main[4]/div[7]/div[1]/div[3]/div[4]/div[5]/div[1]/div[3]/jsl/a[1]/span[1]/span[2]'
wait = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,xpath)))
load_all_flights = driver.find_element_by_xpath(xpath)
load_all_flights.click()
soup = BeautifulSoup(driver.page_source, 'html.parser')
info = soup.find_all('div', class_="gws-flights-results__collapsed-itinerary gws-flights-results__itinerary")
for trip in info:
price = trip.find('div', class_="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price")
if price == None:
price = trip.find('div', class_="flt-subhead1 gws-flights-results__price")
type_of_flight = trip.find('div', class_="gws-flights-results__stops flt-subhead1Normal gws-flights-results__has-warning-icon")
if type_of_flight == None:
type_of_flight = trip.find('div', class_="gws-flights-results__stops flt-subhead1Normal")
print(str(type_of_flight.text).strip() + " : " + str(price.text).strip())
解决方案
推荐阅读
- css - 没有媒体查询或 calc() 函数的响应式电子邮件 [Office 365 和新的 Outlook.com]
- arrays - 用Vue js计算数组中某个类别的过滤项的长度
- json - 解析 json 以从两个对象中生成表
- go - 调用 Close() 时,Kafka 消费者卡在 ReadMessage 上
- android - 是否可以在 Android 上强制或找出本机分辨率?
- javascript - 对于函数中的默认 DATE 参数,我必须在 Oracle 中检查哪些数据库参数?
- android - 当我更新我的 Android Studio 时,我得到一个字符串数组无法解析错误
- c# - 无法将文件从 Angular 8 上传到 .net core 2.2?
- python - 是否可以在 Azure Web App (Linux) 上的主机操作系统上安装包
- conda - 在检查目录属性时,conda 环境的磁盘使用量似乎比实际更大?