4

I'm new in Selenium with Python. I'm trying to scrape some data but I can't figure out how to parse outputs from commands like this:

driver.find_elements_by_css_selector("div.flightbox")

I was trying to google some tutorial but I've found nothing for Python.

Could you give me a hint?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

6

find_elements_by_css_selector() would return you a list of WebElement instances. Each web element has a number of methods and attributes available. For example, to get an inner text of the element, use .text:

for element in driver.find_elements_by_css_selector("div.flightbox"):
    print(element.text)

You can also make a context-specific search to find other elements inside the current element. Taking into account, that I know what site you are working with, here is an example code to get the departure and arrival times for the first-way flight in a result box:

for result in driver.find_elements_by_css_selector("div.flightbox"):
    departure_time = result.find_element_by_css_selector("div.departure p.p05 strong").text
    arrival_time = result.find_element_by_css_selector("div.arrival p.p05 strong").text

    print [departure_time, arrival_time]
  

Make sure you study Getting Started, Navigating and Locating Elements documentation pages.

bomben
  • 590
  • 6
  • 16
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    @Milan you are welcome. See if we can "close" the previous topic too. Happy parsing :) – alecxe Jun 19 '15 at 19:03
  • Hi @Alecxe, could I ask if you could take a look at my question: https://stackoverflow.com/questions/60832771/python-and-selenium-extract-the-information-in-a-div-class-to-a-json-object-or; I tried your suggestion in 'Update 5' of my question (I'm trying different approaches, but still struggling), if you had the time, I would appreciate it. – Slowat_Kela Mar 24 '20 at 16:49