推荐学习书目
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 · Oct 19, 2019 · 2095 views
    This topic created in 2474 days ago, the information mentioned may be changed or developed.
    有这样一段代码下拉框,选择分类:

    <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 实现选择某分类,可惜弄了半天也没琢磨出来,望大神来帮帮我呀,感激不尽。
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3390 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:58 · PVG 18:58 · LAX 03:58 · JFK 06:58
    ♥ Do have faith in what you're doing.