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

selenium 淘宝流量充值输入手机号出现问题, 请问如何解决?

  •  
  •   woshichuanqilz · 2017-10-01 11:58:00 +08:00 · 5062 次点击
    这是一个创建于 2371 天前的主题,其中的信息可能已经有所发展或是发生改变。

    对应的元素名字是:

    <input id="J_phone" type="text" name="_fm.ecard._0.m" value="" class="ipt tel-input" maxlength="11" autocomplete="off">
    

    代码是这样的:

    driver.find_element_by_id('J_phone').send_keys('手机号 xxx')

    但是这个位置什么东西也没有写进去, 请问这个问题如何处理:

    pic

    似乎没有找到那个元素: 然后出现问题。

    pic

    J__LinkBuy 是购买的链接, 点击之后跳转到新的页面

    pic

    这个是相对完整的代码:

    element=WebDriverWait(driver,60).until(lambda driver :
    driver.find_element_by_css_selector('#J_LinkBuy'))
    
    element.click()
    time.sleep(5)
    
    driver.find_element_by_id('J_phone').send_keys(d['phone'])
    
    
    19 条回复    2017-10-13 00:15:13 +08:00
    laoyur
        1
    laoyur  
       2017-10-01 12:29:08 +08:00
    不要用 time.sleep(5),要等到那个元素 clickable 时再 send_keys
    ljcarsenal
        2
    ljcarsenal  
       2017-10-01 12:31:25 +08:00
    为啥要自动化操作
    woshichuanqilz
        3
    woshichuanqilz  
    OP
       2017-10-01 12:37:32 +08:00
    @laoyur 五秒已经够了, 足够加载出来了
    代码改成这样会有这个问题:

    ```
    element=WebDriverWait(driver,60).until(lambda driver :
    driver.find_element_by_css_selector('#J_LinkBuy'))
    ```
    ![pic]( http://7xpvdr.com1.z0.glb.clouddn.com/1001123700.png
    )
    woshichuanqilz
        4
    woshichuanqilz  
    OP
       2017-10-01 12:43:24 +08:00
    @ljcarsenal 懒得点那么多按键。 ~~ 顺便学学 selenium
    xsephiroth
        5
    xsephiroth  
       2017-10-01 12:52:05 +08:00 via Android
    应该不是同一个 frame, 你将 selenium 里那个源码打出来查一下, 另外 chrome 里调试 console 里选 top 层
    woshichuanqilz
        6
    woshichuanqilz  
    OP
       2017-10-01 13:40:40 +08:00
    @xsephiroth 您好您指的是那个源码? 能指导一下吗, 我可以付费的。
    xsephiroth
        7
    xsephiroth  
       2017-10-01 15:57:29 +08:00 via Android
    @woshichuanqilz
    Driver 里打出来应该和 chrome 里加载后的源码是一致的,你查一下 iframe 应该就能看到了
    woshichuanqilz
        8
    woshichuanqilz  
    OP
       2017-10-01 21:51:57 +08:00
    @xsephiroth 恩恩 按照你的方法做了, 出问题的时候这个 page_source 是空值.

    这个是为什么?

    print(driver.page_source)
    woshichuanqilz
        9
    woshichuanqilz  
    OP
       2017-10-09 09:27:50 +08:00
    @xsephiroth 你好请问有没有好的解决办法~ 我可以付费解决
    linw1995
        10
    linw1995  
       2017-10-11 09:07:49 +08:00
    @woshichuanqilz #9 https://stackoverflow.com/questions/37687149/how-to-switch-to-iframe-in-selenium
    我来拿赏金,这里讲了如何切换到 iframe 的方法
    sansansan333
        11
    sansansan333  
       2017-10-11 09:08:06 +08:00
    没用过 selenium 不过看你这个描述,像是往里写的时候元素还没有加载出来吧。这里面能写判断和 console 吗。能的话先跑个定时器试试能不能出来这个元素不就解决了?
    Shazoo
        12
    Shazoo  
       2017-10-11 09:41:48 +08:00
    @woshichuanqilz

    一般用了 selenium 都找不到的元素,就是页面在其他 iframe 里面。

    你看看之前的一个旧帖,结合其他答案,就知道如何切换 iframe 了。

    https://www.v2ex.com/t/358770#reply8
    CosimoZi
        13
    CosimoZi  
       2017-10-11 09:42:32 +08:00
    估计是 iframe 问题
    capo
        14
    capo  
       2017-10-11 10:18:20 +08:00
    甩个网页链接呗
    linhua
        15
    linhua  
       2017-10-11 11:03:44 +08:00
    @woshichuanqilz
    建议用 pycharm 下断点调试,调试时,可以单独执行 代码块
    或者 使用 selenium 的浏览器插件
    blueorange
        16
    blueorange  
       2017-10-11 11:17:23 +08:00
    selenium 是可以打印截图的 打印截图之后 你看看有没有那个输入框。
    linhua
        17
    linhua  
       2017-10-11 15:26:07 +08:00
    @woshichuanqilz
    运行了一下,你的这个代码是可以的!
    你的 cookie 是怎么处理的?
    woshichuanqilz
        18
    woshichuanqilz  
    OP
       2017-10-12 23:40:49 +08:00
    @Shazoo 尝试过切换 iframe 是无效的
    woshichuanqilz
        19
    woshichuanqilz  
    OP
       2017-10-13 00:15:13 +08:00
    @linw1995 应该不是 iframe 的问题因为整个页面的内容都没有
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5305 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.