Selenium的基本使用(Python)

import time, os, sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, TimeoutException


class WebDriver(object):
    """浏览器驱动类"""
    def __init__(self, url):
        self.url = url
        self.driver = webdriver.Chrome()

    def do_input(self):
        try:
            self.driver.get(self.url)
            self.driver.maximize_window()
            search = self.driver.find_element_by_id('kw')
            search.send_keys('Python')
            search.send_keys(Keys.ENTER)
            wait = WebDriverWait(self.driver, 10)
            wait.until(EC.presence_of_element_located((By.ID, 'content_left')))

            print(self.driver.current_url)
            print(self.driver.get_cookies())
            print(self.driver.page_source)

            # 交互动作
            source = self.driver.find_element_by_css_selector('#draggable')
            target = self.driver.find_element_by_css_selector('#dropable')
            # 执行拖拽操作
            ActionChains(self.driver).drag_and_drop(source, target).perform()

            # 执行JavaScript代码
            self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
            self.driver.execute_script('alert("To Bottom")')

            #获取元素信息
            logo = self.driver.find_element_by_id('zh-top-link-logo')
            print(logo.get_attribute('class'))

            # 获取文本内容
            input = self.driver.find_element_by_class_name('zu-top-add-position')
            print(input.text)

            # 获取ID, 位置,标签名,大小
            input = self.driver.find_element_by_class_name('zu-top-add-position')
            print(input.id)
            print(input.location)
            print(input.tag_name)
            print(input.size)

            # Frame的切换和使用
            self.driver.switch_to_frame('leftFrame')
            try:
                logo = self.driver.find_element_by_class_name('logo')
            except NoSuchElementException as e:
                print('NO LOGO: ', e.Message)
            self.driver.switch_to_default_content()
            logo = self.driver.find_element_by_class_name('logo')
            print(logo.text)

            # 隐式等待, 当没有找到元素时将继续等待,未出现元素时开启等待,超过等待时间抛出异常。
            self.driver.implicitly_wait(10)

            # 显示等待
            wait = WebDriverWait(self.driver, 10)

            # 查找输入框
            edit = wait.until(EC.presence_of_element_located((By.ID, 'q')))
            if edit:
                self.driver.find_element_by_id('q').send_keys('apple')
            # 元素是否可点击
            search = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'btn-search')))
            if search:
                self.driver.find_element_by_css_selector('btn-search').click()


            #  前进/后退
            self.driver.forward()
            self.driver.back()

            # cookies
            self.driver.get_cookies()
            self.driver.add_cookie({'name': 'lijz', 'id': '123456', 'gender': 'F'})
            self.driver.get_cookies()
            self.driver.get_cookie('name')
            self.driver.delete_all_cookies()
            self.driver.get_cookies()

            # 选项卡管理
            # 执行js脚本切换选项卡
            self.driver.execute_script('window.open()')
            self.driver.switch_to_window(self.driver.window_handles[1])
            self.driver.get('http://www.baidu.com')
            self.driver.switch_to_window(self.driver.window_handles[0])
            self.driver.get('https://python.org')

            # 异常处理
            try:
                self.driver.get("http://www.baidu.com")
            except TimeoutException as e:
                print(e.Message)
            
            try:
                self.driver.find_element_by_id('hello')
            except NoSuchElementException as e:
                print(e.Message)
            finally:
                self.driver.close()
            


        except Exception as e:
            print("Input Fail ***")
        finally:
            self.driver.close()

if __name__ == '__main__':
    url = 'https://www.baidu.com'
    WebDriver(url).do_input()






展开阅读全文

没有更多推荐了,返回首页