最实用的selenium使用指南

发表于:2024-1-11 09:48

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

 作者:mutreei    来源:稀土掘金

  selenium使用
  一 环境搭建
  下载selenium
  pip install selenium
  下载浏览器驱动(以Edge为例)
  在设置中找到当前Edge版本号,在[Microsoft Edge WebDriver - Microsoft Edge Developer](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)中下载对应版本驱动。
  将下载的压缩包解压后得到驱动的exe文件,将该文件拖到python安装文件夹的script文件夹下。
  在浏览器中的使用:
  示例
  from selenium.webdriver import Edge # 引入
  web = Edge() # 创建浏览器对象
  web.get("https://www.baidu.com") # 执行操作,打开百度
  二 selenium的简单使用
  1. 引入引擎和需要的类
  2. 创建浏览器对象
  3. 打开网页,可以选中元素操纵和获取信息
  示例
  from selenium.webdriver import Edge
  from selenium.webdriver.common.keys import Keys
  import time
  # 创建浏览器对象
  web = Edge()
  # 打开一个网页
  web.get("https://www.lagou.com")
  # 可以使用xpath 类名 样式查找element
  web.find_element('xpath', '//*[@id="changeCityBox"]/p[1]/a').click()
  # 由于加载需要一点时间,所以需要等待
  time.sleep(1)
  web.find_element('xpath', '//*[@id="search_input"]').send_keys('python', Keys.ENTER)
  time.sleep(1)
  web.find_element('xpath', '//*[@id="jobList"]/div[1]/div[1]/div[1]/div[1]/div[1]/a').click()
  time.sleep(1)
  # 切换selenium操作的页面
  web.switch_to.window(web.window_handles[-1])
  job_detail = web.find_element('xpath', '//*[@id="job_detail"]/dd[2]/div').text
  print(job_detail)
  # 关闭当前页面
  web.close()
  # 切回原窗口
  web.switch_to.window(web.window_handles[0])
  注意iframe的存在:
  # 如果源码中有iframe的话,是没有办法直接拿到数据的,必须先切换到iframe中再操作
  frame = web.find_element('xpath', '//*[@id="g_iframe"]')
  web.switch_to.frame(frame)
  注意操作select:
  1. 下拉列表应当先引用下拉列表的支持
  from selenium.webdriver.support.select import Select
  2. 拿到select元素并进行包装
  sel_el = web.find_element_by_xpath('//*[@id="OptionDate"]')
  # 对元素进行包装, 包装成下拉菜单
  sel = Select(sel_el)
  3. 使用条件切换选项,拿到每个选项中的不同数据
  for i in range(len(sel.options)):  # i就是每一个下拉框选项的索引位置
      sel.select_by_index(i)  # 按照索引进行切换
      time.sleep(2)
      table = web.find_element_by_xpath('//*[@id="TableList"]/table')
      print(table.text)  # 打印所有文本信息
      print("===================================")
  三  无头浏览器
  可以通过参数配置的方式来不打开浏览器也能拿到数据
  如果有被iframe包裹的话需要切换到iframe里面才能拿到数据
  from selenium.webdriver import Edge
  from selenium.webdriver.edge.options import Options#引入设置项
  import time
  # 准备好参数配置
  opt = Options()
  opt.add_argument("--headless")
  opt.add_argument("--disable-gpu")
  web = Edge(options=opt)  # 把参数配置设置到浏览器中
  web.get("https://music.163.com/#/song?id=1430620302")
  time.sleep(2)
  iframe = web.find_element('xpath', '//*[@id="g_iframe"]')
  web.switch_to.frame(iframe)# 切换到frame中拿取
  # 如何拿到页面代码Elements(经过数据加载以及js执行之后的结果的html内容)
  print(web.page_source)
  Options基础配置
  1. user-agent
  2. 代理
  3. 不加载图片和css
  opt = Options()
  # 无头浏览器
  opt.add_argument("--headless")
  opt.add_argument("--disable-gpu")
  # 不加载图片和css
  prefs = {"profile.managed_default_content_settings.images": 2,
           'permissions.default.stylesheet': 2}
  opt.add_experimental_option('prefs', prefs)
  # 设置user-Agent
  opt.add_argument('user-agent=' + UserAgent().random)  # 初始化一个别的User-Agent
  # IP池
  proxy_arr = [
      '--proxy-server=http://111.3.118.247:30001',
      '--proxy-server=http://183.247.211.50:30001',
      '--proxy-server=http://122.9.101.6:8888',
  ]
  proxy = random.choice(proxy_arr)  # 随机选择一个代理
  print(proxy)  # 如果某个代理访问失败,可从proxy_arr中去除
  opt.add_argument(proxy)  # 添加代理
  # 添加配置
  driver = Edge(options=opt)
  四 其他实用操作
  如果你的程序被识别到了怎么办?
  1.chrome的版本号如果小于88  在你启动浏览器的时候(此时没有加载任何网页内容), 向页面嵌入js代码. 去掉webdriver
  web = Chrome()
  web.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
     navigator.webdriver = undefined
      Object.defineProperty(navigator, 'webdriver', {
        get: () => undefined
      })
    """
  })
  web.get(xxxxxxx)
  2.chrome的版本大于等于88
  option = Options()
  # option.add_experimental_option('excludeSwitches', ['enable-automation'])
  option.add_argument('--disable-blink-features=AutomationControlled')
  web操作:
  1. 移动到某一位置点击:
  from selenium.webdriver import Edge
  from selenium.webdriver.common.action_chains import ActionChains
  web = Edge()
  verify_img_element = web.find_element_by_xpath('//*[@id="J-loginImg"]')
  ActionChains(web).move_to_element_with_offset(verify_img_element, x, y).click().perform()   # perform提交
  2. 拖拽:
  btn = web.find_element_by_xpath('//*[@id="nc_1_n1z"]')
  ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号