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脚本使界面自动下拉滚动。

目录
相关文章
|
1月前
|
安全 编译器 开发者
Python语言的配置解释器
Python语言的配置解释器
|
1月前
|
JavaScript 前端开发 测试技术
Python Selenium基本用法
Python Selenium基本用法
29 2
|
4天前
|
前端开发 测试技术 C++
Python自动化测试面试:unittest、pytest与Selenium详解
【4月更文挑战第19天】本文聚焦Python自动化测试面试,重点讨论unittest、pytest和Selenium三大框架。unittest涉及断言、TestSuite和覆盖率报告;易错点包括测试代码冗余和异常处理。pytest涵盖fixtures、参数化测试和插件系统,要注意避免过度依赖unittest特性。Selenium的核心是WebDriver操作、等待策略和测试报告生成,强调智能等待和元素定位策略。掌握这些关键点将有助于提升面试表现。
18 0
|
8天前
|
存储 API Python
python之代理ip的配置与调试
python之代理ip的配置与调试
|
13天前
|
Web App开发 测试技术 网络安全
|
1月前
|
Web App开发 前端开发 JavaScript
Python Selenium是一个强大的自动化测试工具
Python Selenium是一个强大的自动化测试工具
|
1月前
|
Web App开发 iOS开发 Python
Selenium安装与配置
Selenium是一个用于Web应用程序测试的自动化工具。它直接运行在浏览器中,模拟真实用户的操作。Selenium支持多种主流浏览器,如IE、Mozilla Firefox、Safari、Google Chrome、Opera和Edge等。在爬取数据的时候对于需要登录后才能爬取的情况往往可以利用Selenium来进行模拟登录,登录后进行数据的获取。这里先介绍Selenium的安装与配置,实现通过python来驱动浏览器进行操作。
29 2
|
1月前
|
Web App开发 测试技术 Python
使用 Python 结合 Selenium 访问一个 url
使用 Python 结合 Selenium 访问一个 url
26 0
|
1月前
|
Web App开发 安全 定位技术
关于使用 Python 和 Selenium chrome driver 访问 url 时修改 source ip 的问题
关于使用 Python 和 Selenium chrome driver 访问 url 时修改 source ip 的问题
60 0
|
2月前
|
Web App开发 数据采集 前端开发
基于Python的Selenium详解:从入门到实践
基于Python的Selenium详解:从入门到实践
106 0

热门文章

最新文章