Python 调用 selenium chromedriver 常用配置

简介: 话不多说,上示例代码from selenium import webdriverimport timechromedriver = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.

话不多说,上示例代码

from selenium import webdriver
import time

chromedriver = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"

#设置不加载图片
chrome_opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_opt.add_experimental_option("prefs", prefs)

dirver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_opt)

dirver.get("https://www.zhihu.com/signup?next=%2F")

#获取页面HTML
html = driver.page_source

dirver.find_element_by_xpath("//div[@class='SignContainer-switch']/span").click()

dirver.find_element_by_xpath("//div[@class='SignFlow-accountInput Input-wrapper']/input").send_keys("")

dirver.find_element_by_xpath("//div[@class='SignFlow-password']/div/div[@class='Input-wrapper']/input").send_keys("")

dirver.find_element_by_xpath("//button[@class='Button SignFlow-submitButton Button--primary Button--blue']").click()

cookie = [item["name"] + "=" + item["value"] for item in dirver.get_cookies()]

cookiestr = ';'.join(item for item in cookie)

print(cookiestr)

上述代码是登录知乎的示例,

其中,send_keys("")填写的是用户名和密码。
首先点击find_element_by_xpath("//div[@class='SignContainer-switch']/span")这个元素切换到输入用户名和密码的界面,
然后使用xpath找到用户名和密码的元素位置,执行send_keys("")方法填充用户名和密码,
最后找到登录按钮,执行点击动作。

cookie = [item["name"] + "=" + item["value"] for item in dirver.get_cookies()]
这行代码是获取登录后的cookie。

js = 'function scroll(){var scrollElem=document.scrollingElement;scrollElem.scrollTop = len;le = document.scrollingElement.scrollTop;He = document.scrollingElement.scrollHeight;cl = document.scrollingElement.clientHeight;if(le + cl == He&&le + cl!=0) {}len=len+300;setTimeout(scroll,500)};len = 0;scroll();'
driver.execute_script(js)

上边两行代码是执行js脚本使界面自动下拉滚动。

目录
相关文章
|
4月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
411 0
|
7月前
|
Web App开发 存储 前端开发
Python+Selenium自动化爬取携程动态加载游记
Python+Selenium自动化爬取携程动态加载游记
|
5月前
|
SQL 测试技术 数据库
healenium+python+selenium
上次介绍了如何利用healenium+java+selenium来实现selenium的自愈,这次介绍如何healenium+python+selenium。关于healenium+python+selenium网上资料更少,并且甚至是错误的。在著名的书籍《软件测试权威指南中》也是有一定问题的。现在介绍如下
300 4
|
5月前
|
JSON 缓存 开发者
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
本文详解淘宝开放平台taobao.item_get接口对接全流程,涵盖参数配置、MD5签名生成、Python企业级代码实现及高频问题排查,提供可落地的实战方案,助你高效稳定获取商品数据。
|
9月前
|
数据采集 Web App开发 JavaScript
基于Selenium的Python爬虫抓取动态App图片
基于Selenium的Python爬虫抓取动态App图片
715 68
|
6月前
|
JavaScript 前端开发 机器人
【Azure Bot Service】在中国区Azure上部署机器人的 Python 版配置
本文介绍了在中国区Azure上使用Python SDK配置Azure Bot Service时遇到的问题及解决方案,涵盖参数设置与适配器配置,适用于希望在Azure中国区部署Python机器人的开发者。
199 6
|
10月前
|
IDE 开发工具 开发者
手把手教你安装PyCharm 2025:开发者的Python IDE配置全流程+避坑指南
本教程详细介绍了PyCharm 2025版本在Windows系统下的安装流程及配置方法,涵盖AI代码补全与智能调试工具链等新功能。内容包括系统要求、安装步骤、首次运行配置(如主题选择与插件安装)、创建首个Python项目,以及常见问题解决方法。此外,还提供了切换中文界面和延伸学习资源的指导,帮助用户快速上手并高效使用PyCharm进行开发。
5320 61
|
9月前
|
Python
在VScode环境下配置Python环境的方法
经过上述步骤,你的VSCode环境就已经配置好了。请尽情享受这扇你为自己开启的知识之窗。如同你在冒险世界中前行,你的探索之路只有越走越广,你获得的知识只会越来越丰富,你的能力只会越来越强。
900 37
|
9月前
|
数据采集 Web App开发 前端开发
Python+Selenium爬虫:豆瓣登录反反爬策略解析
Python+Selenium爬虫:豆瓣登录反反爬策略解析

推荐镜像

更多