如何解析Selenium驱动程序元素? [英] How to parse Selenium driver elements?

查看:93
本文介绍了如何解析Selenium驱动程序元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Selenium中的Python新手.我正在尝试抓取一些数据,但是我不知道如何解析这样的命令的输出:

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")

我正在尝试搜索一些教程,但对Python却一无所获.

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

能给我一个提示吗?

推荐答案

find_elements_by_css_selector()将为您返回

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.

这篇关于如何解析Selenium驱动程序元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆