Selenium_WebDriver登录模拟鼠标移动切换窗体等操作练习(cssSelector初练手)_Java
cssSelector
据说cssSelector比xpath快。
所以,有固定ID属性的页面元素用By.id或者By.cssSelector(“#id属性值”)来找,有class属性优先用By.cssSelector(“.class属性值”)的就尽量用cssSelector(#代表id=。.代表class=)。
例如以下博客有具体介绍cssSelector:
http://blog.csdn.net/hwm831002/article/details/8424239
http://blog.csdn.net/tenfyguo/article/details/5780970
窗体切换
当我们点击linktext时或者点击某些button,提交某些表单时(不能一概而论。须要看具体页面的),浏览器会新开一个窗体打开,但此时WebDriver实例事实上还指向前一个页面。普通情况,我们都是想让WebDriver指向新开页面的,此时须要切换窗体。
//用WindowHandle的方式获取新开页面
String currentWindow = dr.getWindowHandle();
Set<String> handles = dr.getWindowHandles();
Iterator h = handles.iterator();
while(h.hasNext()){
dr = dr.switchTo().window((String) h.next());
}
或者依据页面title来进行切换
//用WindowHandle+页面title来切换dr至我们想要的窗体
currentWindow = dr.getWindowHandle();
handles = dr.getWindowHandles();
//System.out.println(handles.size());
h = handles.iterator();
while(h.hasNext()){
dr = dr.switchTo().window((String) h.next());
//System.out.println(dr.getTitle());
if (dr.getTitle().equals("查找好友 - 挖財社区"))
break;
}
须要注意的是:假设用WebDriver.get(URL)方式或者WebDriver.navigate().to(URL)的方式打开URL时。浏览器并不会新开窗体,此时就不用切换WebDriver窗体。
非典型下拉菜单的选择
上图是某財BBS右上角搜索框的一个下拉菜单。它明显不是一个典型的下拉选择框,用经常使用的Seector是无法实现它的选择的。
自己实验了好久,最终发现,这样的须要先点击展开下拉菜单。然后将鼠标移动到用户上,此时用户会变红,再点击。才干够选择上。
为了模拟的更像,选择完了之后。再将鼠标移动到旁边别的地方。点击一下,收起下拉菜单。
^_^
它的html源代码例如以下
<a hidefocus="true" onclick="showMenu(this.id)" class="showmenu xg1" id="comiis_twtsc_type" href="javascript:;" initialized="true">帖子/a>
<ul style="position: absolute; z-index: 301; left: 1090px; top: 86.5px; display: none;" class="p_pop" id="comiis_twtsc_type_menu" initialized="true">
<li>
<a class="curtype" rel="forum" href="javascript:;">帖子</a></li>
<li>
<a rel="group" href="javascript:;">財友会</a>
</li>
<li>
<a rel="collection" href="javascript:;">专辑</a>
</li>
<li>
<a rel="article" href="javascript:;">资讯</a>
</li>
<li>
<a rel="user" href="javascript:;">用户</a>
</li>
</ul>
java代码例如以下,act.moveByOffset(20,30)表示往右下移动
//点开搜索类型下拉框,将鼠标移动到用户上并选择
Actions act = new Actions(dr);
WebElement dropDown = dr.findElement(By.cssSelector("a.showmenu.xg1"));
WebElement user = dr.findElement(By.cssSelector("ul#comiis_twtsc_type_menu>li>a[rel='user']"));
act.click(dropDown).perform();
act.moveToElement(user).click().perform();
act.moveByOffset(20,30).click().perform();
代码块
总体代码例如以下。实现:
- 某財BBS自己主动登录
- 打开理財子版块
- 点击图标关闭左側导航栏
- 选择搜索用户
- 输入关键字并搜索
- 切换窗体
- 输出全部搜索到的包括关键字的username
package pkg;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by Sophie on 15/7/4.
*/
public class AutoLoginSearchTest {
public WebDriver dr;
public String url;
public void setUp(WebDriver dr, String url){
this.dr = dr;
this.url = url;
this.dr.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
public WebDriver login(AutoLoginSearchTest a) throws Exception {
//登录
a.dr.get(a.url);
a.dr.findElement(By.id("account")).clear();
a.dr.findElement(By.id("account")).sendKeys("******");
a.dr.findElement(By.id("pwd")).clear();
a.dr.findElement(By.id("pwd")).sendKeys("******");
a.dr.findElement(By.id("login-btn")).click();
return a.dr;
}
public WebDriver switchWindow(WebDriver dr)throws Exception{
//打开理財规划子版块
dr.findElement(By.linkText("理財规划")).click();
//用WindowHandle的方式获取新开页面
String currentWindow = dr.getWindowHandle();
Set<String> handles = dr.getWindowHandles();
Iterator h = handles.iterator();
while (h.hasNext()) {
dr = dr.switchTo().window((String) h.next());
}
System.out.println(dr.getCurrentUrl());
dr.manage().window().maximize();
//然后关闭左側导航栏
dr.findElement(By.cssSelector("a.comiis_left_closes")).click();
return dr;
}
public WebDriver mouseMove(WebDriver dr)throws Exception{
//点开搜索类型下拉框,将鼠标移动到用户上并选择
Actions act = new Actions(dr);
WebElement dropDown = dr.findElement(By.cssSelector("a.showmenu.xg1"));
WebElement user = dr.findElement(By.cssSelector("ul#comiis_twtsc_type_menu>li>a[rel='user']"));
act.click(dropDown).perform();
act.moveToElement(user).click().perform();
act.moveByOffset(20, 30).click().perform();
return dr;
}
public WebDriver search(WebDriver dr)throws Exception{
//找到搜索输入框,并输入关键字,然后点击搜索button
WebElement input = dr.findElement(By.id("comiis_twtsc_txt"));
input.clear();
input.sendKeys("周杰伦");
dr.findElement(By.id("comiis_twtsc_btn")).click();//Click之后FireFox新开了一个页面
//用WindowHandle+页面title来切换dr至我们想要的窗体
String currentWindow = dr.getWindowHandle();
Set<String> handles = dr.getWindowHandles();
//System.out.println(handles.size());
Iterator h = handles.iterator();
while (h.hasNext()) {
dr = dr.switchTo().window((String) h.next());
//System.out.println(dr.getTitle());
if (dr.getTitle().equals("查找好友 - 挖財社区"))
break;
}
//xpath模糊查找
List<WebElement> searchResult = dr.findElements(By.xpath("//a[contains(@title,'周杰伦')]"));
System.out.println("搜索到" + searchResult.size() + "个周杰伦粉丝,他们各自是:");
for (int i = 0; i < searchResult.size(); i++) {
System.out.println(searchResult.get(i).getAttribute("title"));
}
return dr;
}
public void tearDown(WebDriver dr) throws Exception {
dr.quit();
}
public static void main(String[] args) throws Exception {
WebDriver ff = new FirefoxDriver();
String url ="https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php";
AutoLoginSearchTest autoLoginSearch = new AutoLoginSearchTest();
autoLoginSearch.setUp(ff,url);
WebDriver afterLogin = autoLoginSearch.login(autoLoginSearch);
WebDriver childForum = autoLoginSearch.switchWindow(afterLogin);
WebDriver mouseAction = autoLoginSearch.mouseMove(childForum);
WebDriver searchResult = autoLoginSearch.search(mouseAction);
autoLoginSearch.tearDown(searchResult);
}
}
哈哈。第一次用CSDN的MarkDown。爽歪!!!再也不用原来那个难受的控件来写博文了~~还有,第一次用IDEA,爽歪!。!Eclipse简直叫人太汗了。。。
Selenium_WebDriver登录模拟鼠标移动切换窗体等操作练习(cssSelector初练手)_Java的更多相关文章
- C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作
C# Timer用法及实例详解 关于C# Timer类 在C#里关于定时器类就有3个 C# Timer使用的方法1.定义在System.Windows.Forms里 C# Timer使用的方法2.定 ...
- selenuim2模拟鼠标键盘操作
有时候有些元素不便点击或者做其他的操作,这个时候可以借助selenium提供的Actions类,它可以模拟鼠标和键盘的一些操作,比如点击鼠标右键,左键,移动鼠标等操作.对于这些操作,使用perform ...
- selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等
selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...
- winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已解] 望一起讨论)(技术改变世界-cnblog)
http://www.cnblogs.com/IAmBetter/archive/2012/01/14/2322156.html winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已 ...
- selenium webdriver(4)---模拟鼠标键盘操作
webdriver提供Actions来模拟鼠标悬浮.拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Action ...
- windows7如何用键盘模拟鼠标操作
windows7如何用键盘模拟鼠标操作 https://jingyan.baidu.com/article/6dad5075104907a123e36e38.html 听语音 37453人看了这个视频 ...
- WEBBROWSER中模拟鼠标点击(SendMessage/PostMessage)
好久没有写文章,发一篇顶顶博客访问量.别人建议转一些比较好的代码也贴过来,但是我打算这里主要发自己原创的代码,所以么..流量该多少就多少吧... 回到主题,在webbrowser中点击某链接网上几乎都 ...
- Python3从零开始爬取今日头条的新闻【四、模拟点击切换tab标签获取内容】
Python3从零开始爬取今日头条的新闻[一.开发环境搭建] Python3从零开始爬取今日头条的新闻[二.首页热点新闻抓取] Python3从零开始爬取今日头条的新闻[三.滚动到底自动加载] Pyt ...
- py库: pyautogui (自动测试模块,模拟鼠标、键盘动作)
PyAutoGUI 是一个人性化的跨平台 GUI 自动测试模块 pyautogui 库 2017-10-4 pip install pyautogui python pip.exe install p ...
随机推荐
- 疑似CPU或者内存故障导致进程崩溃
我们有一个服务跑在微软云的所有宿主机上.最近发现某一台机器上该服务进程持续崩溃.崩溃原因是访问了一个无效指针,对应的代码如下 serviceListIniBuffer.AppendF("Se ...
- 你不得不看的Python机器学习工具
IEEE Spectrum排行榜第一,Skill UP排名第一的开发工具,Stack Overflow年度调查中程序员最感兴趣的选择,Stack Overflow 6月份访问量最多的编程语言..... ...
- MySQL索引与Index Condition Pushdown
实际上,这个页面所讲述的是在MariaDB 5.3.3(MySQL是在5.6)开始引入的一种叫做Index Condition Pushdown(以下简称ICP)的查询优化方式.由于本身不是一个层面的 ...
- Spring JDBC 示例
在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常, ...
- Oracle函数sys_connect_by_path 详解
Oracle函数sys_connect_by_path 详解 语法:Oracle函数:sys_connect_by_path 主要用于树查询(层次查询) 以及 多列转行.其语法一般为: s ...
- css实现左侧固定宽,右侧自适应的7中方法
一个面试会问的问题,如何实现两个盒子,左侧固定宽度,右侧自适应. 1.利用 calc 计算宽度的方法 css代码如下: .box{overflow: hidden;height: 100px;marg ...
- 利用vertical-align实现行内元素对齐
实际项目中,常常会遇到一排行内元素对齐排列的需求,但是往往它们是这样的 我们想要的其实是这样的 曾经我一度不得不使用定位来实现我想要的位置效果,将父元素设置 position:relative ,行内 ...
- Python开发目录
Python开发目录 Python之三目运算符 Python之文件的基本操作
- test only
Test TinyMCE only , Test Link
- Jrebel热部署配置完整教程(IntelliJ IDEA、Jrebel、spring boot、springboot、eclipse、Tomcat)
标签:IntelliJ IDEA.Jrebel.spring boot.springboot.eclipse.Tomcat1.安装插件并激活插件安装参考:http://blog.csdn.net/u0 ...