selenium集成edge浏览器

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.keys import Keys
import time

class SeleniumHelper:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, locator, timeout=10):
        """
        查找单个元素
        :param locator: 元素定位信息,如(By.ID, 'element_id')
        :param timeout: 超时时间,默认为10秒
        :return: WebElement对象或None
        """
        try:
            element = WebDriverWait(self.driver, timeout).until(
                EC.presence_of_element_located(locator)
            )
            return element
        except (TimeoutException, NoSuchElementException):
            return None

    def find_elements(self, locator, timeout=10):
        """
        查找多个元素
        :param locator: 元素定位信息,如(By.CLASS_NAME, 'element_class')
        :param timeout: 超时时间,默认为10秒
        :return: WebElement对象列表或空列表
        """
        try:
            elements = WebDriverWait(self.driver, timeout).until(
                EC.presence_of_all_elements_located(locator)
            )
            return elements
        except (TimeoutException, NoSuchElementException):
            return []

# Edge浏览器 https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
s=Service('C:/webdriver/msedgedriver.exe')
options = webdriver.EdgeOptions()
options.use_chromium = True
# 屏蔽inforbar
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
options.add_argument("start-maximized")
# options.add_argument('--headless')
options.binary_location=r'C:\Program Files (x86)\Microsoft\Edge Dev\Application\msedge.exe'
driver = webdriver.Edge(service=s, options=options)
driver.get('https://www.baidu.com')
# 创建SeleniumHelper对象
helper = SeleniumHelper(driver)
search_box = helper.find_element((By.ID, 'kw'))
if search_box:
    search_box.send_keys("aaaaaa")
    search_box.send_keys(Keys.RETURN)
time.sleep(6)
driver.quit()

你可能感兴趣的:(selenium,edge,python)