使用selenium访问爱奇艺网站

栏目: 编程工具 · 发布时间: 4年前

内容简介:selenium 是一种常用的自动化测试工具。它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Selenium 的插件,还可以通过录制,快速生成脚本。selenium 支持多种主流的开发语言,比如python3.7.3

使用 selenium 访问爱奇艺网站

selenium 是一种常用的自动化测试工具。它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Selenium 的插件,还可以通过录制,快速生成脚本。

selenium 支持多种主流的开发语言,比如 Rubyjavapythonjavascript

环境搭建

python3.7.3

运行 pip install selenium 就可以直接下载最新的selenium版本

准备

浏览器:chrome 70.0.3538.77

操作系统:win7

selenium版本: 3.14.1

chromedriver: https://npm.taobao.org/mirrors/chromedriver/70.0.3538.97/

使用selenium 打开和关闭浏览器

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.iqiyi.com/")
driver.quit()

定位搜索框

search_xpath=r"//*[@id='nav_searchboxIn']/input"
driver.find_element_by_xpath(search_xpath).send_keys("复仇者联盟")

点击搜索图片

search_button=r"//*[@id='nav_searchboxOut']/span"
driver.find_element_by_xpath(search_button).click()

切换tab页

#导入键盘操作--20190528更新
from selenium.webdriver.common.keys import Keys
#此处通过键盘操作切换tab页
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t")
#all_handles 保存所有已经打开的tab窗体
all_handles = driver.window_handles
print(driver.window_handles)
index_handle=driver.current_window_handle
print(index_handle)
#用switch_to方法切换到tab窗体
for handle in all_handles:
    if handle!=index_handle:
        print('now is search window')
        search_handle = handle
driver.switch_to.window(search_handle)

打印页面的 title ,并截图

print(driver.title)
driver.get_screenshot_as_file("aqiyi.png")

总结

本文主要介绍了自动化工具 selenium 的基本使用,如何对页面元素进行基本操作,实现自动抓取关键字图片功能。


以上所述就是小编给大家介绍的《使用selenium访问爱奇艺网站》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacker's Delight

Hacker's Delight

Henry S. Warren Jr. / Addison-Wesley / 2002-7-27 / USD 59.99

A collection useful programming advice the author has collected over the years; small algorithms that make the programmer's task easier. * At long last, proven short-cuts to mastering difficult aspec......一起来看看 《Hacker's Delight》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具