薄情先生|Selenium web自动化之web控件交互( 二 )


// 拖拽测试@Testpublicvoid dragTest() throws InterruptedException {//跳转测试网页webDriver.get("");// 通过id获取位置actions.dragAndDrop(webDriver.findElement(By.id("dragger")),webDriver.findElement(By.xpath("//*[@class='item'][last()]"))).perform();Thread.sleep(3000);}
Actions 用法4模拟按键
薄情先生|Selenium web自动化之web控件交互只支持火狐
@Testpublicvoid keytest() throws InterruptedException {//跳转测试网页webDriver.get("");// 通过xpath 上下文webDriver.findElements(By.xpath("//input[@type='textbox']")).get(0).sendKeys("ceshi");//全选actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();//crtl+c 复制actions.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();// ctrl+vactions.keyDown(webDriver.findElements(By.xpath("//input[@type='textbox']")).get(0),Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();Thread.sleep(3000);}Actions 用法5输入某个字符点击搜索并翻页
薄情先生|Selenium web自动化之web控件交互定位到百度一下 搜索框id
薄情先生|Selenium web自动化之web控件交互获取下一页的位置 通过获取a标签后带class=n 得到按钮位置
publicvoid socrll(){try {//跳转测试网页webDriver.get("");// 通过xpath 上下文webDriver.findElement(By.id("kw")).sendKeys("今日头条");TouchActions actions = new TouchActions(webDriver);actions.click(webDriver.findElement(By.id("su"))); // 点击百度一下JavascriptExecutor js= (JavascriptExecutor) webDriver;// 驱动强转为js驱动js.executeScript("window.scrollBy(0,document.body.scrollHeight)");//滑动到底部Thread.sleep(2000);webDriver.findElement(By.xpath("//a[@class='n']")).click();Thread.sleep(3000);}catch (Exception e){e.printStackTrace();}}


推荐阅读