python - 使用 python 在 selenium 中修改 div 的类
问题描述
我正在尝试将网页的页面布局从网格列表更改为简单列表,但问题不是下拉列表或按钮。它正在改变向 div 元素添加活动类我如何在 python 中使用 selenium 来实现这一点
<a href="#" class="dropdown-item title_mode active" id="0"><span class="fas fa-th-large fa-fw " aria-hidden="true"></span> Detailed</a>
<a href="#" class="dropdown-item title_mode " id="1"><span class="fas fa-th-large fa-fw " aria-hidden="true"></span> grid Detailed</a>
<a href="#" class="dropdown-item title_mode " id="2"><span class="fas fa-th-large fa-fw " aria-hidden="true"></span>list Detailed</a>
<a href="#" class="dropdown-item title_mode " id="3"><span class="fas fa-th-large fa-fw " aria-hidden="true"></span>simple Detailed</a>
解决方案
您可以尝试driver.execute_script
使用 Javascript 修改类的功能。像这样的东西:
driver.execute_script("""
document.getElementById('0').classList.remove('active');
document.getElementById('1').classList.add('active');
""")
根据网站的设置方式,仅修改类可能不起作用。
奇怪的是,我找不到该函数的任何文档可供参考,但它在 selenium-python 文档的常见问题解答中有所提及
推荐阅读
- c++ - 为什么 cmake 安装的文件与实际构建的二进制文件不同?
- javascript - 使用javascript将pdf文件的每一页转换为图像时出现问题
- express - Heroku 使用 Options 而不是 POST 方法
- javascript - 动态添加的输入字段未在 request.form 中提交
- python - 如何在 Python 中基于 Datetime 合并 DataFrame
- linux - 如何杀死linux中的模式进程
- python-3.x - 为什么当我在我的 FastAPI 服务中启动 uvicorn 时,我的配置方法会运行两次?
- html - 如何使用jsp在包含html文件中设置类
- c# - 字符串长度的高位标志
- javascript - 变量布尔值在 @Input 和 *ngif 中不起作用