V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
vtoexsir
V2EX  ›  Python

怎么使用 splinter 模块的 find_by()方法来获取 360 搜索主页上的'搜索按钮'元素?

  •  
  •   vtoexsir · 2018-08-24 13:32:06 +08:00 · 3249 次点击
    这是一个创建于 2044 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python 有个模块:splinter,其中有个实例方法:find_by().
    怎么使用这个方法取到 https://www.so.com 网页上那个搜索按钮呢?
    from splinter import Browser
    browser = Browser('chrome')
    ele = browser.find_by() #这句代码怎么写?
    ele.click()
    注:搜索按钮的源码如下: <input type="submit" id="search-button" class="skin-search-button" value="搜索">
    ele = browser.find_by_id("search-button")
    如上代码可以取到 ele 元素,但我想要的是:怎么使用 find_by()方法取到这个按钮元素.
    多谢您回复!

    5 条回复    2018-08-24 16:48:26 +08:00
    Xiaobaixiao
        1
    Xiaobaixiao  
       2018-08-24 13:49:53 +08:00
    官方文档:
    Splinter provides 6 methods for finding elements in the page, one for each selector type: css, xpath, tag, name, id, value, text. Examples:

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

    楼主哪里看的 find_by()方法?
    vtoexsir
        2
    vtoexsir  
    OP
       2018-08-24 15:10:44 +08:00
    @Xiaobaixiao #1 查看安装文件,相对地址:python-3.6.3\Lib\site-packages\splinter\driver\webdriver\__init__.py
    第 363 行:
    def find_by(self, finder, selector, original_find=None, original_query=None):
    elements = None
    end_time = time.time() + self.wait_time

    func_name = getattr(getattr(finder, _meth_func), _func_name)
    find_by = original_find or func_name[func_name.rfind('_by_') + 4:]
    query = original_query or selector

    while time.time() < end_time:
    try:
    elements = finder(selector)
    if not isinstance(elements, list):
    elements = [elements]
    except NoSuchElementException:
    pass

    if elements:
    return ElementList(
    [self.element_class(element, self) for element in elements],
    find_by=find_by, query=query)
    return ElementList([], find_by=find_by, query=query)
    Xiaobaixiao
        3
    Xiaobaixiao  
       2018-08-24 15:38:50 +08:00
    #1 所说的 6 个方法 都是返回这个函数调用的…… ,既然你都懂得看源码了,那按照参数传递进去就行了。
    话说为什么不直接使用人家封装好的方法呢
    vtoexsir
        4
    vtoexsir  
    OP
       2018-08-24 15:52:33 +08:00
    @Xiaobaixiao #3 我一瓶不满半瓶晃荡!!!
    就是不知道 find_by 方法的各个参数怎么写?
    所以来提问,求个实例代码的!
    Xiaobaixiao
        5
    Xiaobaixiao  
       2018-08-24 16:48:26 +08:00
    @vtoexsir 举个荔枝,当你调用 browser.find_by_css('h1') 的时候,实际是调用 browser.find_by(driver.find_elements_by_css_selector,'h1',original_find="css",original_query=css_selector),
    所以如果你打算直接用 find_by(),你要确定 selector 的类型,对照参数传入就是了。
    如果说错了,望 dalao 们指正
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2884 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 11:17 · PVG 19:17 · LAX 04:17 · JFK 07:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.