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()






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIJZ_Python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值