赞
踩
网上大部分资料都是推荐使用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的路径即可。
到这里就完成的配置准备工作
下面看测试代码:
- from selenium import webdriver
- from selenium.webdriver.edge.service import Service
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
-
- driverfile_path = 'D:\\18223\\Microsoft Visual Studio\\msedgedriver.exe'#路径,改成自己存放的位置
- service = Service(executable_path=driverfile_path)
- one_driver = webdriver.Edge(service=service)
-
- try:
- one_driver.get('https://www.bilibili.com/')
- # 使用显式等待代替隐式等待
- search_box = WebDriverWait(one_driver, 10).until(
- EC.presence_of_element_located((By.XPATH, "//input[@type='text'][@autocomplete='off']"))
- )
- search_box.send_keys('日本核废水')#在搜索框输入
-
- search_button = WebDriverWait(one_driver, 10).until(
- EC.element_to_be_clickable((By.XPATH, "//input[@type='submit']"))
- ) # 确保使用正确的选择器定位按钮
- search_button.click()
- # 这里的10是等待时间,10后无操作自动退出
-
-
-
-
- except Exception as e:
- print(f"An error occurred: {e}")
-
- finally:
- one_driver.quit()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
运行得到以下视图
搞定!
本文参考Python+Selenium+Edge浏览器安装与简单运行(2/2)_edgecsspath安装-CSDN博客,一名优秀的博主,结合自己安装的实际问题,对测试代码进行了一定的优化,希望可以帮助到相同问题的小伙伴们!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。