Andrea Barnabò
Andrea Barnabò

Reputation: 564

Selenium Timeout Exception

I am trying to scrape the cheapest flight price from Google Flight search page. I got a timeout error, even if I use WebDriverWait. This is my code:

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

example_url = 'https://www.google.com/flights?hl=it#flt=/m/07_pf./m/01f62.2019-05-24;c:EUR;e:1;0px:2;sd:1;t:f;tt:o'

def get_price(url):
        driver = webdriver.Firefox()
        driver.get(url)
        try:
             wait = WebDriverWait(driver, 20)
             element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, '.gws-flights-results__cheapest-price')))
             print(element)
        finally:
             price = driver.find_element_by_css_selector('.gws-flights-results__cheapest-price').text
             driver.quit()
        price = float(price.split(' ')[0])
        driver.quit()
        return price       


price = get_price(example_url)
print(price)

I got this error:

Traceback (most recent call last):
  File "semplice.py", line 23, in <module>
    price = get_price(example_url)
  File "semplice.py", line 13, in get_price
    element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, '.gws-flights-results__cheapest-price')))
  File "/home/andrea/ENTER/lib/python3.4/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

No message has been printed on the terminal. Just this is the message. Where is the problem ?

Upvotes: 1

Views: 2089

Answers (2)

Marcel Wilson
Marcel Wilson

Reputation: 4572

@kafels is correct, you don't need the dot in front of a classname locator. However, once you fix that, you don't really need to do the 2nd search in the finally.

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

example_url = 'https://www.google.com/flights?hl=it#flt=/m/07_pf./m/01f62.2019-05-24;c:EUR;e:1;0px:2;sd:1;t:f;tt:o'

def get_price(url):
    driver = webdriver.Firefox()
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    try:
        element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 
                                                             'gws-flights-results__cheapest-price')))
    except Exception as exc:
        raise
    finally:
        # no need to look for the element here since you do it in the try section.
        driver.quit()

    print(element)
    price = element.text
    price = float(price.split(' ')[0])
    return price

price = get_price(example_url)
print(price)

Upvotes: 2

Kafels
Kafels

Reputation: 4059

The error it is because you're passing the dot to find the element by class name:

element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, '.gws-flights-results__cheapest-price'))

Change it to:

element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'gws-flights-results__cheapest-price')))

Upvotes: 1

Related Questions