Splinter 查找元素

通常的查找元素方法

browser.find_by_css('h1')
browser.find_by_xpath('//h1')
browser.find_by_tag('h1')
browser.find_by_name('name')
browser.find_by_text('Hello World!')
browser.find_by_id('firstheader')
browser.find_by_value('query')

方法返回的结果是一个list,通过以下方法可以返回第一个、最后一个或指定位置的元素。

first_found = browser.find_by_name('name').first
last_found = browser.find_by_name('name').last
second_found = browser.find_by_name('name')[1]

All elements and find_by_id

A web page should have only one id, so the find_by_id method returns always a list with just one element.

If you need to find the links in a page, you can use the methods find_link_by_text, find_link_by_partial_text, find_link_by_href or find_link_by_partial_href. Examples:

links_found = browser.find_link_by_text('Link for Example.com')
links_found = browser.find_link_by_partial_text('for Example')
links_found = browser.find_link_by_href('http://example.com')
links_found = browser.find_link_by_partial_href('example')

As the other find_* methods, these returns a list of all found elements.
You also can search for links using other selector types with the methods find_by_css, find_by_xpath, find_by_tag, find_by_name, find_by_value and find_by_id.

链式查询元素

Finding methods are chainable, so you can find the descendants of a previously found element.
find的方法是链式的,所以可以在找到一个元素之后,继续在这个元素内部查找你所要找的元素

divs = browser.find_by_tag("div")
within_elements = divs.first.find_by_name("name")

ElementDoesNotExist exception

If an element is not found, the find_* methods return an empty list. But if you try to access an element in this list, the method will raise the splinter.exceptions.ElementDoesNotExist exception.

转载于:https://www.cnblogs.com/windanchaos/p/6398797.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值