最实用的selenium使用指南

 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()

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:


这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 

 

  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值