selinum自己笔记

http://selenium-release.storage.googleapis.com/index.html

浏览器操作

iframe是否有handle
Xpath格式和cssSelector格式
handle切换


设置driver
System.setProperty(“webdriver.chrome.driver”,“xx”);
WebDriver driver = new ChromeDriver();


manage对象

窗口最大化
driver.manage().window().maximize(); 包含书签栏
driver.manage().window().fullscreen(); F11

获取窗口的左起点位置
driver.manage().window().getPosition() setPosition
获取窗口的尺寸
driver.manage().window().getSize() setSize


navigate对象
页面后退
driver.navigate().back();
页面前进
driver.navigate().forward();
页面刷新
driver.navigate().refresh();
页面切换
driver.navigate().to(arg0);


driver对象

关闭当前窗口
driver.close()

退出驱动关闭所有窗口
driver.quit()

获取当前页面网址
driver.getCurrentUrl()

获取页面源码
driver.getPageSource()

获取页面的title - head标签里的title标签
driver.getTitle()

获取句柄对象
driver.getWindowHandle()

获取drver中所有的句柄对象
driver.getWindowHandles()

切换到frame
driver.switchTo().frame(elm)
切换到页面
driver.switchTo().window(string)


获取单个元素
driver.findElement(By.xx)
//获取一些元素
driver.findElements(By.xx)

Xpath定位
driver.findElement(By.xpath(""))

//a[@id=‘hello’] id=hello的任意一个a元素
//a[@class=‘hello’] class=hello的任意一个a元素
//a[@name=‘hello’] name=hello的任意一个a元素

//div[@id=‘world’]/a[@id=‘hello’] id=world的任意一个div下的 - id=hello的任意一个a元素
//div[@id=‘world’ and @class=‘nice’] 联合查找某个元素

xx/div[last()] 倒数第一个div
xx/div[last()-1] 倒数第二个div
xx/preceding-sibling::a[2] 往上的第2个a
xx/following-sibling::a[2] 往下的第2个a

xx/book[position()❤️] 后边2个book
xx/book[@lang=‘eng’] 属性lang=值eng的book

input[starts-with(@id,‘ctrl’)]
input[ends-with(@id,’_userName’)]
Input[contains(@id,‘userName’)]
xx//a[contains(text(), ’退出’)]

xx/… xx的上一级元素
//title | //price 选取文档中的所有 title 和 price 元素。

classSelector定位
driver.findElement(By.cssSelector("")

#hello id=hello
.world class = world
[name=wd] name = wd
html>body>div>a 每一级查找
div.nice > a#hello id=nice的div 下的 class=hello的a

通过link text定位
driver.findElement(By.linkText("")) 链接的文案内容

partialLinkText定位
driver.findElement(By.partialLinkText("")) 模糊查找内容 最好具有唯一性

标签名定位
driver.findElement(By.TagName(""))

页面滑动
1 JS - 页面滑动条滑动
窗口的“顶部”与对象的“顶端” 对齐
((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true);”, element);
窗口的“底部”与对象的“顶端” 对齐
((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(false);”, element);
移动到窗口绝对位置坐标
((JavascriptExecutor) driver).executeScript(“window.scrollTo(0, 1600)”);
移动到页面最底部
((JavascriptExecutor) driver).executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
相对当前的坐标移动 移动
((JavascriptExecutor) driver).executeScript(“window.scrollBy(0, 700)”);

2
Robot r = new Robot();
r.mouseWheel(100);

Robot类用于为需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot 的主要目的是便于 Java 平台实现自动测试。
出现证书 可以选择ESC取消 或者 ENTER确定
Robot r = new Robot();
//按下确定键并释放
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

void keyPress(int keycode) 按下指定的键
void keyRelease(int keycode) 释放指定的键
void mouseMove(int x,int y) 将鼠标移动到给定的屏幕坐标上
void mousePress(int buttons) 按下一个或多个鼠标按键
void mouseRelease(int buttons) 释放一个活多个鼠标按键
void mouseWheel(int wheelAmt) 滚动鼠标滑轮

按键常量
VK_HOME Home键 VK_CONTROL 控制键
VK_END End键 VK_SHIFT shift键
VK_PGUP page up键 VK_BACK_SPACE 退格键
VK_PGDN page down键 VK_CAPS_LOCK 大小写锁定键
VK_UP 上箭头 VK_NUM_LOCK 小键盘锁定键
VK_DOWN 下箭头 VK_ENTER 回车键
VK_LEFT 左箭头 VK_UNDEFINED 未知键
VK_RIGHT 右箭头 VK_F1–VK_F12 F1 – F12
VK_ESCAPE Esc键 VK_0 --VK_9 0 — 9
VK_TAB Tab键 VK_A --VK_Z A----Z


Actions对象 鼠标操作的行为
Actions action = new Actions(driver);

action.click(elm); 左键点击
action.contextClick(elm); 右键点击
action.doubleClick(elm); 双击
action.clickAndHold(A); 点击并维持着点击的状态
action.dragAndDrop(A,B); 从A元素拖拽到B元素位置
action.dragAndDropBy(A,左右,上下); 把A拖拽固定距离

dragAndDropBy方法就是clickAndHold和moveByOffset
 actions.dragAndDropBy(dragTable, 270, 170).perform();
   actions.clickAndHold(dragTable).moveByOffset(270, 170).click().perform();

action.moveToElement(A) 鼠标移入 (A,X,Y) 移入到A的某个位置
action.moveByOffset(x,y) 鼠标移动
action.release(); 鼠标释放

动作调用完毕以后必须调用perform()方法才能运行。

组合键 action.click(search).keyDown(Keys.SHIFT).sendKeys(“222”).keyUp(Keys.SHIFT).perform();


WebElement对象
getLocation() 获取页面上的位置
getSize()
getCssValue(“margin-left”)
getAttribute(“id”)
sendKeys(“输入内容”)
click
submit 提交
clear
isDisplayed 是否展示
isEnabled 是否可用
isSelected 是否被选中
getTagName 获取标签名
getText 获取文本

常见异常
NoSuchElementException:没有找到元素

NoSuchFrameException:没有找到iframe

NoSuchWindowException:没找到窗口句柄handle

NoSuchAttributeException:属性错误

NoAlertPresentException:没找到alert弹出框

ElmentNotVisibleException:元素不可见

ElementNotSelectableException:元素没有被选中

TimeoutException:查找元素超时

你可能感兴趣的:(selinum)