使用Selenium访问爱奇艺网站

发表于:2019-6-04 10:53

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:凭海临风的IT江湖    来源:凭海临风博客

分享:
  使用selenium访问爱奇艺网站
  selenium 是一种常用的自动化测试工具。它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Selenium 的插件,还可以通过录制,快速生成脚本。
  selenium 支持多种主流的开发语言,比如Rubyjava,python,javascript。
  环境搭建
  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的基本使用,如何对页面元素进行基本操作,实现自动抓取关键字图片功能。

     上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号