Selenium在打印结果之前并不等待单击按钮

2024-05-15 12:12:44 发布

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

我试图在点击显示航班完整列表的按钮后,使用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())

Tags: fromimportdivdriverseleniumfindpricexpath