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
oIMOo
V2EX  ›  Python

Python selenium wait until 的实现

  •  
  •   oIMOo · 2019-05-16 21:34:37 +08:00 · 3748 次点击
    这是一个创建于 1798 天前的主题,其中的信息可能已经有所发展或是发生改变。

    经过查阅文档,wait until 需要调用有 __call__ 方法的值。

    我的情况是想检查 <div class='title'> piggy </div> 中的 piggy 是否出现。

    读了 文档 之后,并没有发现哪个方法是检查内容的。

    这个 title 不是网页的最高级别 title,只是页面中的一个 div,所以 title_istitle_contains 并不适用。

    目前使用的办法是:

    time_now = time.time()
    while(time_now - time.time() < timeout_max):
        title_find = browser.find_element_by_class_name('title')
        if (title_find and title_find.text == 'piggy'):
            break
        else:
            if (time_now - time.time() >= timeout_max):
            	Timeout
    do something
    

    求教:肯定有更工整的办法吧?

    第 1 条附言  ·  2019-05-17 00:00:50 +08:00

    搞定啦

    检测 div 出现:EC.presence_of_element_located((By.ID, 'id_name'))

    检测 div 内容:EC.text_to_be_present_in_element((By.CLASS_NAME, 'class_name'), 'piggy')

    检测按钮:EC.element_to_be_clickable((By.ID, 'button_id'))

    查找 checkbox: EC.presence_of_element_located((By.XPATH,"//input[@value='choose_me']"))

    检查是否选择: browser.find_element_by_xpath("//input[@value='choose_me']").is_selected()

    检查是否有alert:EC.alert_is_present()

    第 2 条附言  ·  2019-05-17 00:13:49 +08:00
    新问题来了,这些操作用 requests 包不能做么?
    8 条回复    2019-05-17 00:31:11 +08:00
    tomoya92
        1
    tomoya92  
       2019-05-16 21:42:03 +08:00 via iPhone
    oIMOo
        3
    oIMOo  
    OP
       2019-05-16 22:00:45 +08:00
    @tomoya92 @fakeman
    谢谢两位,目标网页:[点击这里]( http://www.rdv-etrangers.meurthe-et-moselle.gouv.fr/eAppointment54-etrangers/appointment.do?preselectservice=SEJOUR)
    我的代码```WebDriverWait(browser, 5).until(EC.presence_of_element_located_value((By.CLASS_NAME, 'title'), 'Introduction'))```
    然而通过不了,所以我才用的主题里的笨办法……
    oIMOo
        4
    oIMOo  
    OP
       2019-05-16 22:09:39 +08:00
    @oIMOo
    (EC.text_to_be_present_in_element_value((By.CLASS_NAME, 'title'), 'Introduction'))
    这个可以哈哈哈,我写错方法了……
    oIMOo
        5
    oIMOo  
    OP
       2019-05-16 22:10:48 +08:00
    @oIMOo 现在在研究怎么替换这一句:

    if (browser.find_element_by_xpath('//*[@title="suivant"]')):
    itskingname
        6
    itskingname  
       2019-05-16 22:28:42 +08:00
    用 By.XPATH, 并把 XPath 写为:

    //div[@class="title" and contains(text(), "piggy")]
    oIMOo
        7
    oIMOo  
    OP
       2019-05-16 23:08:56 +08:00
    @itskingname
    谢谢~~ 这个搞定了~
    目前正在替换怎么检测是否存在某个 checkbox

    原代码:if (browser.find_element_by_xpath("//input[@value='75']")):, 网页第二页
    dcalsky
        8
    dcalsky  
       2019-05-17 00:31:11 +08:00
    requests 是发 http 请求用的呀,压根不可能知道网页长什么样有什么东西啊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4108 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:13 · PVG 13:13 · LAX 22:13 · JFK 01:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.