selenium
1 环境配置
from selenium import webdriver
driver = webdriver.Chrome()
URL = "http://www.baidu.com"
driver.get(URL)
driver.maximize_window()
driver.implicitly_wait(80)
'''
Test code
'''
driver.quit()
2 元素定位
element = driver.find_element(by=By.ID, value=value)
element = driver.find_element(by=By.NAME, value=value)
element = driver.find_element(by=By.CLASS_NAME, value=value)
element = driver.find_element(by=By.TAG_NAME, value=value)
element = driver.find_element(by=By.LINK_TEXT, value=value)
element = driver.find_element(by=By.PARTIAL_LINK_TEXT, value=value)
element = driver.find_element(by=By.XPATH, value=value)
element = driver.find_element(by=By.CSS_SELECTOR, value=value)
By.ID
, By.NAME
, By.CLASS_NAME
底层调用 By.CSS_SELECTOR
2.1 XPath定位
/nodename/
3 操作
3.1 元素操作
# click
element.click()
# input,upload files
element.send_keys(value)
# clear
element.clear()
3.2 浏览器操作
driver.maximize_window()
driver.minimize_window()
driver.set_window_size(width=width, height=height)
driver.set_window_position(x=x, y=y)
driver.back()
driver.forward()
driver.refresh()
driver.close()
driver.quit()
driver.title
driver.current_url
3.3 元素信息
element.size
element.text
element.get_attribute(name=name)
element.is_displayed()
element.is_enabled()
element.is_selected()
3.4 鼠标操作
类方法:ActionChains()
# 导入类
from selenium.webdriver.common.action_chains import ActionChains
# 实例化对象
action = ActionChains(driver)
# 添加动作
# 右击
action.context_click(element)
# 双击
action.double_click(element)
# 拖拽
action.drag_and_drop(element)
# 悬停
action.move_to_element(element)
# 执行动作
action.perform()
3.5 键盘操作
# 导入类
from selenium.webdriver.common.keys import Keys
# 使用
# 删除
element.send_keys(Keys.BACK_SPACE)
element.send_keys(Keys.BACKSPACE)
# 空格
element.send_keys(Keys.SPACE)
# 制表
element.send_keys(Keys.TAB)
# 回退
element.send_keys(Keys.ESCAPE)
# 回车
element.send_keys(Keys.ENTER)
# 全选(ctrl+a)
element.send_keys(Keys.CONTROL,'a')
# 复制(ctrl+c)
element.send_keys(Keys.CONTROL,'c')
4 元素等待
4.1 隐式等待
driver.implicitly_wait(timeout) # 单位 秒(s)
4.2 显式等待
# 导包
from selenium.webdriver.support.wait import WebDriverWait
webDriverWait = WebDriverWait(
driver=driver, timeout=timeout, poll_frequency=poll_frequency)
element = webDriverWait.until(method=method) # method是寻找元素的方法
# 常常简写,并用匿名函数完成
element = WebDriverWait(driver=driver, timeout=timeout, poll_frequency=poll_frequency).until(
lambda driver: driver.find_element(by=by, value=value))
5 下拉选择框
5.1 通过CSS选择器定位
driver.find_element(by=By.CSS_SELECTOR,value="[value='value']").click()
5.2 通过Select
类
element=driver.find_element(by=by,value=value)
select=Select(element)
select.select_by_index(index=index)
select.select_by_value(value=value)
select.deselect_by_visible_text(text=text)
6 弹出框
alert = driver.switch_to.alert # alert对象是Alert类的一个示例
alert.text
alert.accept()
alert.dismiss()
7 滚动条
js = "window.scrollTo(0,1000)"
driver.execute_script(js)
8 切换框架
在HTML5中,推荐使用iframe而不是frame
driver.switch_to.frame(frame_reference=frame_reference) # frame_reference 是 frame 的id,name或者为frame的Element实例
driver.switch_to.default_content() # 回到默认页面
9 多窗口切换
窗口切换依赖于窗口句柄
# 当前窗口句柄
driver.current_window_handle
# 所有窗口句柄
driver.window_handles
# 切换窗口
driver.switch_to.window(handle)
10 窗口截图
driver.get_screenshot_as_file(imgpath)
11 cookie
driver.get_cookie(name)
driver.get_cookies()
driver.add_cookie(cookie_dict=cookie_dict)
posted on 2024-04-01 14:20 RandolphChen 阅读(10) 评论(0) 编辑 收藏 举报