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

Python selenium 中如何使用 JavaScript 直接选择下拉框的某个选项呢?

  •  
  •   firearmer · 2019-10-19 10:37:20 +08:00 · 1723 次点击
    这是一个创建于 1649 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有这样一段代码下拉框,选择分类:

    <div class="widgets-selects direction-horizontal">
    <div class="widgets-selects_container">选择分类<i class=""></i></div>
    <div class="widgets-selects_select_container" style="display: none;">
    <a href="javascript:;" class="">社会</a>
    <a href="javascript:;" class="">国内</a>
    <a href="javascript:;" class="">国际</a>
    <a href="javascript:;" class="">体育</a>
    <a href="javascript:;" class="">科技</a>
    <a href="javascript:;" class="">娱乐</a>
    <a href="javascript:;" class="">军事</a>
    </div>
    </div>
    </div>


    选择了第一个分类“社会”后,代码变成了这样:

    <div class="widgets-selects direction-horizontal">
    <div class="widgets-selects_container" data-spm-anchor-id="a2s0i.db_video_write.content.i2.4a603caaqY4ZC8">社会<i class=""></i></div>
    <div class="widgets-selects_select_container" style="display: none;">
    <a href="javascript:;" class="selected" data-spm-anchor-id="a2s0i.db_video_write.content.6">社会</a>
    <a href="javascript:;" class="" data-spm-anchor-id="a2s0i.db_video_write.content.7">国内</a>
    <a href="javascript:;" class="" data-spm-anchor-id="a2s0i.db_video_write.content.8">国际</a>
    <a href="javascript:;" class="" data-spm-anchor-id="a2s0i.db_video_write.content.9">体育</a>
    <a href="javascript:;" class="" data-spm-anchor-id="a2s0i.db_video_write.content.10">科技</a>
    <a href="javascript:;" class="" data-spm-anchor-id="a2s0i.db_video_write.content.11">娱乐</a>
    <a href="javascript:;" class="" data-spm-anchor-id="a2s0i.db_video_write.content.12">军事</a>
    </div>
    </div>
    </div>

    之前我用 python selenium 的操作是用 click()命令点击一下“选择分类”,弹出下拉框,再在其中 click()点击选择某个分类。但我很希望能在 python 中用 driver.execute_script()命令直接调用 JavaScript 实现选择某分类,可惜弄了半天也没琢磨出来,望大神来帮帮我呀,感激不尽。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4852 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:39 · PVG 13:39 · LAX 22:39 · JFK 01:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.