selenium的简单使用

2019-02-27  本文已影响0人  Eoccc
例子demo
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec

# 打开浏览器
URL = 'https://www.baidu.com'
browser = webdriver.Firefox()  #  打开浏览器
# 设置等待加载时间
wait = WebDriverWait(browser, 5)  
browser.get(URL)
wait.until(Ec.presence_of_element_located((By.ID,'kw')))
# 获取网页中的元素,填充数据以及点击事件
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
browser.quit()
设置浏览器的地址
browser = webdriver.Firefox(executable_path="浏览器的地址") 
浏览器的基本操作
最大化: browser.maximize_window()
弹窗: browser.switch_to_alert()
刷新: browser.forward()
后退: browser.backward()
刷新: browser.refresh()
关闭: browser.close()  # 关闭当前窗口
退出: browser.quit()
在浏览器中找到的元素,webdriver提供了一系列的对象定位方法,browser.find_element_by_属性(),常用的属性有以下几种:

· id
· name
· class_name
· link_text
· partial_link_text
· tag_name
· xpath
· css_selector

在浏览器中找到页面元素以后,对它做的一些操作:

WebElement 类

  1. 点击该元素: click()
  2. 清除该元素原有的文字: clear()
  3. 给该元素填写新的文字: send_keys()
  4. 获取该元素的文字: text
  5. 获取该元素的指定属性: get_attribute()
  6. 获取当前元素的某一种属性:getAttribute(attribute)
  7. 获取当前元素的文字属性:getText()
  8. 获取当前元素的 Displayed 属性: isDisplayed()
selenium.webdriver.common.by.By类,By的后面可选的属性:
CLASS_NAME = 'class name'
CSS_SELECTOR = 'css selector'
ID = 'id'
LINK_TEXT = 'link text'
NAME = 'name'
PARTIAL_LINK_TEXT = 'partial link text'
TAG_NAME = 'tag name'
XPATH = 'xpath'

browser.find_element(By.ID, "q")
selenium.webdriver.support.expected_conditions类,
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as Ec

Ec.presence_of_element_located((By.ID,'kw'))
Ec.presence_of_all_elements_located((By.ID, 'query'))
Ec.element_to_be_clickable()
Ec.title_is('title')
Ec.title_contains('title')  # 判断页面标题包含title
# locator, eg: (By.ID, 'erwx')
Ec.visibility_of_element_located(locator)  # 等待locator元素可见
Ec.invisibility_of_element_located(locator)  # 等待locator元素隐藏
Ec.visibility_of(driver.find_element_by_link_text('高级搜索'))
Ec.text_to_be_present_in_element((By.ID, 'query'), 'text')  # 等待locator的元素中包含text文本
Ec.text_to_be_present_in_element_value((By.ID, 'query'), 'selenium')  # 等待locator元素的value属性为value
Ec.frame_to_be_available_and_switch_to_it((By.ID, 'frame'))  # 等待frame可切入
Ec.alert_is_present()  # 等待alert弹出窗口出现
Ec.element_to_be_selected(element)  # 等待element元素是被选中
Ec.element_selection_state_to_be(element, is_selected) #等待element元素的值被选中为is_selected(布尔值)
等待网页加载
  1. 强制等待
    一直等待到设置的时间后才执行下一步
import time
time.sleep(5)  # 等待5秒
  1. 隐式等待
    最长等待设置的时间, 若在设置的时间内完成加载, 则可以在加载完后继续执行下一步
browser.implicitly_wait(30)  # 隐性等待,等30秒
  1. 显性等待
    selenium.webdriver.support.wait.WebDriverWait,配合该类的until()和until_not()方法,能够根据判断条件而进行灵活地等待.
    WebDriverWait(browser, timeout, poll_frequency, ignored_exceptions).until(可执行方法, 超时时返回的信息)
    timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
    poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒
    ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常, 则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。
driver.implicitly_wait(10)  # 隐性等待和显性等待可以同时用,但要注意:等待的最长时间取两者之中的大者
locator =(By.LINK_TEXT, 'link')
WebDriverWait(driver, 20, 0.5).until(Ec.presence_of_element_located(locator))

附一篇大佬写的简书, 非常全面: https://www.jianshu.com/p/5b9fa11a9808

上一篇 下一篇

猜你喜欢

热点阅读