2

I'm very new to python and trying to learn webscraping. Following a tutorial, I'm trying to extract a price from a website but nothing is being printed. What is wrong with my code?

from selenium import webdriver

chrome_path = r"C:\webdrivers\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://reservations.airarabia.com/service-app/ibe/reservation.html#/fare/en/AED/AE/SHJ/KHI/07-09-2019/N/1/0/0/Y//N/N")
price = driver.find_elements_by_class_name("fare-and-services-flight-select-fare-value ng-isolate-scope")
for post in price:
        print(post.text)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
FL44ME
  • 49
  • 4

2 Answers2

1

The first reason for that is because the webpage you are trying to scrape uses javascript to load the HTML so you will need to wait until that element is present to get it using selenium's WebDriverWait

The second reason is that the find_elements_by_class_name method only accepts one class so you would need to either use find_elements_by_css_selector or find_elements_by_xpath

this is how your code should look

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

chrome_path = r"C:\webdrivers\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)

driver.get("https://reservations.airarabia.com/service-app/ibe/reservation.html#/fare/en/AED/AE/SHJ/KHI/07-09-2019/N/1/0/0/Y//N/N")
price = WebDriverWait(driver, 10).until(
    lambda x: x.find_elements_by_css_selector(".currency-value.fare-value.ng-scope.ng-isolate-scope"))

for post in price:
    print(post.get_attribute("innerText"))
painor
  • 1,119
  • 1
  • 8
  • 25
  • Hey. I received the follower errors. [12436:1444:0831/215122.158:ERROR:ssl_client_socket_impl.cc(943)] handshake failed; returned -1, SSL error code 1, net_error -101 Traceback (most recent call last): File "flight.py", line 20, in lambda x: x.find_elements_by_css_selector("fare-and-services-flight-select-fare-value ng-isolate-scope")) File "C:\PYTHON27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – FL44ME Aug 31 '19 at 17:53
  • you can increase the wait if you wish. I put it as 10 sec but if your internet isn't the strongest you can increase it. – painor Aug 31 '19 at 18:04
1

To print the first title you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "isa-flight-select button:first-child span.fare-and-services-flight-select-fare-value.ng-isolate-scope"))).get_attribute("innerHTML"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//isa-flight-select//following::button[contains(@class, 'button')]//span[@class='fare-and-services-flight-select-fare-value ng-isolate-scope']"))).text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Console Output of two back to back execution:

    475
    

You can find a relevant discussion in How to retrieve the title attribute through Selenium using Python?


Outro

As per the documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you so much. I didn't entirely understand the parameters of **EC.visibility_of_element_located**. Was _//isa-flight-select//following::button[contains(@class, 'button')]_ arbitrary? Could you also help with this URL: https://www.ae.cheapflights.com/flight-search/KUL-LON/2020-05-30?sort=price_a - I need to extract the price under class _"price option-text"_, but I'm not sure what I should choose for the visibility criteria or how. – FL44ME Sep 01 '19 at 01:10
  • 1
    @FL44ME i will explain it in a short while but for the other part can you raise a new question as per your new requirement please? – undetected Selenium Sep 01 '19 at 01:17
  • I have posted another – FL44ME Sep 01 '19 at 01:32
  • @FL44ME Did you get a chance to look in to my answer in your _...have posted another..._ question? – undetected Selenium Sep 03 '19 at 08:45