下载msedgedriver.exe
用pip安装库
selenium==3.141
msedge-selenium-tools
demo: 登录哔哩哔哩from msedge.selenium_tools import Edge, EdgeOptions
import json
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
MSEDGE_PATH = r"C:\Program Files (x86)\Microsoft\Edge Beta\Application\msedge.exe"
MSEDGE_DRIVER_PATH = './msedgedriver.exe'
COOKIES_JSON_PATH = './cookies.json'
#这个json文件可以用json.dumps(driver.get_cookies(), indent=2)生成
HREF = 'https://www.bilibili.com/'
def init_imitate_edge_driver() -> Edge:
options = EdgeOptions()
options.use_chromium = True
options.binary_location = MSEDGE_PATH
options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = Edge(options=options, executable_path=MSEDGE_DRIVER_PATH)
driver.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument", {
"source":
"""
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
return driver
def add_default_cookies(driver: Edge,
domain: str = HREF,
cookies_json_path: str = COOKIES_JSON_PATH):
driver.get(domain)
with open(cookies_json_path, 'r') as cookies_json:
for cookie in json.loads(cookies_json.read()):
del cookie['domain']
driver.add_cookie(cookie)
if __name__ == "__main__":
driver = init_imitate_edge_driver()
driver.get(HREF)
#add_default_cookies(driver)
|