当前位置:   article > 正文

python + selenium +Microsoft Edge 自动测试网站_python selenium edge

python selenium edge

网上大部分资料都是推荐使用selenium加Chrome,但现在电脑里面装的都是Microsoft edge,就能不能直接使用电脑现有的配置来实现这个功能呢?

下面看操作:

1.在terminal控制台输入pip install selenium

会显示好安装成功之后,就完成selenium库的安装

2.找到对应的Microsoft edge 版本的驱动器

先打开Microsoft edge中的设置

在关于里面看到版本号为版本 124.0.2478.80 (正式版本) (64 位)

去到Microsoft Edge WebDriver | Microsoft Edge Developer下载相对应的驱动,

在这里我下载的是stable channel x64,找到下载的文件并解压会得到以下的文件,只需要记住meedgedriver.exe的路径即可。

到这里就完成的配置准备工作

下面看测试代码:

  1. from selenium import webdriver
  2. from selenium.webdriver.edge.service import Service
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. driverfile_path = 'D:\\18223\\Microsoft Visual Studio\\msedgedriver.exe'#路径,改成自己存放的位置
  7. service = Service(executable_path=driverfile_path)
  8. one_driver = webdriver.Edge(service=service)
  9. try:
  10. one_driver.get('https://www.bilibili.com/')
  11. # 使用显式等待代替隐式等待
  12. search_box = WebDriverWait(one_driver, 10).until(
  13. EC.presence_of_element_located((By.XPATH, "//input[@type='text'][@autocomplete='off']"))
  14. )
  15. search_box.send_keys('日本核废水')#在搜索框输入
  16. search_button = WebDriverWait(one_driver, 10).until(
  17. EC.element_to_be_clickable((By.XPATH, "//input[@type='submit']"))
  18. ) # 确保使用正确的选择器定位按钮
  19. search_button.click()
  20. # 这里的10是等待时间,10后无操作自动退出
  21. except Exception as e:
  22. print(f"An error occurred: {e}")
  23. finally:
  24. one_driver.quit()

运行得到以下视图

搞定!

本文参考Python+Selenium+Edge浏览器安装与简单运行(2/2)_edgecsspath安装-CSDN博客,一名优秀的博主,结合自己安装的实际问题,对测试代码进行了一定的优化,希望可以帮助到相同问题的小伙伴们!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/728533
推荐阅读
相关标签
  

闽ICP备14008679号