How to parse Selenium driver elements?

2019-07-24 19:04发布

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?

1条回答
戒情不戒烟
2楼-- · 2019-07-24 19:46

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 a 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 are you 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.

查看更多
登录 后发表回答