selenium拖动元素java_【自动化测试】Java+Selenium操作页面元素(合集)

本文基于Java语言,依托于Eclipse工具,使用Selenium框架,主要介绍在Selenium中,如何操作Web页面中的各种元素。

Eclipse 搭建

1.1、Eclipse 配置

1.2、引入依赖包

修改pom.xml文件

在dependencys节点下,添加如下内容,保存即可自动下载

org.seleniumhq.selenium

selenium-java

2.43.1

org.testng

testng

6.9.4

test

org.apache.poi

poi

3.9

访问浏览器

2.1、火狐默认路径:

WebDriver diver = new FirefoxDriver();

2.2、其他浏览器(非默认路径)

谷歌

IE

火狐

访问路径

3.1、访问一个具体的url

调用方法一:driver.get(url);

方法二:driver.navigate().to(url);

浏览器导航

driver.navigate().back();//向前

driver.navigate().forward ();//向后

driver.navigate().refresh();//当前页刷新

关闭浏览器

//关闭当前页面

driver.close();

//关闭所有页面

driver.quit();

页面元素的操作

6.1、输入框

备注:

//找到输入框元素:

WebElement element = driver.findElement(By.id("user"));

//将输入框清空:

element.clear();

//在输入框中输入内容:

element.sendKeys(“test”);

//获取输入框的文本内容:

element.getAttribute("value");

6.2、超链接

6.3、下拉选择框(Select)

对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作

//找到下拉选择框的元素:

Select select = new Select(driver.findElement(By.name("select")));

//选择对应的选择项:

select.selectByVisibleText(“未提交”);

//或

select.selectByValue(“opel”);

//或者通过index选择

select.selectByIndex(1);

//不选择对应的选择项:

select.deselectAll();

select.deselectByValue(“MA_ID_001”);

select.deselectByVisibleText(“mediaAgencyA”);

//或者获取选择项的值:

select.getAllSelectedOptions();//多选列表,list循环可获取到对应的值

select.getFirstSelectedOption().getText();//单选列表直接获取值

6.4、单选项(Radio Button)

//找到单选框元素:

WebElement bookMode =driver.findElement(By.id("BookMode"));

//选择某个单选项:

bookMode.click();

//清空某个单选项:

bookMode.clear();

//判断某个单选项是否已经被选择:

bookMode.isSelected();

6.5、多选项(checkbox)

//多选项的操作和单选的差不多:

WebElement checkbox =driver.findElement(By.id("myCheckbox."));

checkbox.click();

checkbox.clear();

checkbox.isSelected();

checkbox.isEnabled();

6.6、按钮(button)

//找到按钮元素:

WebElement saveButton = driver.findElement(By.id("save"));

//点击按钮:

saveButton.click();

//判断按钮是否enable:

saveButton.isEnabled ();

6.6、左右选择框

也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。

例如:

Select lang = new Select(driver.findElement(By.id("languages")));

lang.selectByVisibleText(“English”);

WebElement addLanguage =driver.findElement(By.id("addButton"));

addLanguage.click();

6.7、弹出对话框(Popup dialogs)

Alert alert = driver.switchTo().alert();

alert.accept();

alert.dismiss();

alert.getText();

6.8、表单(Form)

Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交:

WebElement approve = driver.findElement(By.id("approve"));

approve.click();

//或

approve.submit();//只适合于表单的提交

6.9、上传文件 (Upload File)

//上传文件的元素操作:

WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));

String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";

adFileUpload.sendKeys(filePath);

//拖拉(Drag andDrop)

WebElement element =driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

6.9鼠标悬停(Mouse MoveOn)

Actions builder = new Actions(driver)

builder.moveToElement(driver.findElement(locator)).perform();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268780.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面试题 锁消除是什么

锁消除是在编译器级别的事情。 在即时编译器时,如果发现不可能被共享的对象,则可以消除这些对象的锁操作。 也许你会觉得奇怪,既然有些对象不可能被多线程访问,那为什么要加锁呢?写代码时直接不加锁不就好了。 但是…

通过日志恢复SQL Server的历史数据

园子里前段时间发过一篇通过日志恢复MSSQL数据例子 ,我总结一下 通过日志还原,最重要的是: 1.必须有一个完整的备份,且这个备份必须是在修改、删除数据之前做的。 2.在更新、删除数据之后,做日志备份,该log…

JavaScript面向对象编程指南(五) 原型

第5章 原型 5.1 原型属性 function f(a,b){return a*b;};// length 属性f.length; //2// constructor 构造属性f.constructor; // function Function() { [native code] }// prototype 属性 初始值是空对象typeof f.prototype; //"object" 5.1.1 利用原型添加方法和…

java communal_平台用英语怎么说

平台通常指高于附近区域的平面;楼房的阳台。也指指计算机硬件或软件的操作环境。而在网络上所说的平台一般是指网站。那么你知道平台用英语怎么说吗?下面来学习一下吧。平台英语说法1:platform平台英语说法2:terrace平台英语说法3:flat roof…

面试题-- 什么是偏向锁

所谓的偏向,就是偏心,即锁会偏向于当前已经占有锁的线程 。 大部分情况是没有竞争的(某个同步块大多数情况都不会出现多线程同时竞争锁),所以可以通过偏向来提高性能。即在无竞争时,之前获得锁的线程再次获…

Ping, IPConfig, Format, Netstat, etc. Not Recognized as Commands

Can you use them when you point directly to them..> C:\Windows\System32\ping.exe 127.0.0.1? If so, you probably have a messed up PATH variable.> echo %PATH%Should have "C:\Windows\System32" in it, does it? 以上为google的结果,我…

Collections.min()和Collections.max()的使用

取集合中的最小值 Collections.min(); 取集合中的最大值 Collections.max(); public class TestCollectionMinMax {public static void main(String[] args) {List<Integer> list new ArrayList<>();list.add(3);list.add(7);list.add(9);list.add(1);list.add(…

java date 过时_过时date.toLocaleString()的解决方法

过时date.toLocaleString()的解决方法 Java代码 收藏代码 System.out.println(new java.util.Date()); 输出&#xff1a;Thu Jan 27 14:43:28 CST 2011 Java代码 收藏代码 System.out.println(new java.util.Date().toLocaleString()); 输出&#xff1a;2011-1-27 14:45:…

linux下查看隐藏的文件

在linux下以点开头命名的文件在系统中视为隐藏文件&#xff0c;例如 “.123 ”。用ls、ls -l或者ll是无法查看的。那么如何才能查看linux系统下的隐藏文件呢&#xff1f;下面介绍一下简单的查看方式。1、#在命令行下使用ls -a2、#在XWindow的KDE桌面中在"查看(View)"…

python学习之路(九)

这一节主要讲的是装饰器。装饰器是一个非常好用的&#xff0c;用于装饰已有函数的函数功能。 其优点是不用修改调用方式&#xff0c;还不用修改源代码。 他的思想是&#xff1a;函数既变量&#xff1b;高阶函数&#xff1b;嵌套函数。 现在来尝试写装饰器 有两个函数 各自实现自…

java http请求实现_java工程实现http请求接口

java工程实现http请求接口java工程实现http请求接口1.实现代码package com.home;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.sun.net.httpserver.HttpExchange;import com.sun.net.httpserver.HttpHandler;import com.sun.net.https…

Java集合中removeIf的使用

使用removeIf方法从List中删除元素 在JDK1.8中&#xff0c;Collection以及其子类新加入了removeIf方法&#xff0c;作用是按照一定规则过滤集合中的元素。这里展示removeIf的用法。 需求是过滤掉学生中分数为为18以下的&#xff0c; 一个学生实体类 Data public class Studen…

Java中lombok @Builder注解使用详解

简介 Lombok大家都知道&#xff0c;在使用POJO过程中&#xff0c;它给我们带来了很多便利&#xff0c;省下大量写get、set方法、构造器、equal、toString方法的时间。除此之外&#xff0c;通过Builder注解&#xff0c;lombok还可以方便的实现建造者模式&#xff0c;创建对象 …

鼠标控制

一&#xff09;客户端 using System;using System.Net;using System.Net.Sockets;using System.Text;using System.IO;using System.Threading; namespace 鼠标控制_客户端{ //我们定义一个保存鼠标坐标的结构 public struct MousePosition { private int x; …

java resourcebundle_Java - Properties和ResourceBundle类学习

一、前言在项目的开发过程中&#xff0c;为了统一配置的管理&#xff0c;我们经常需要将一些配置信息根据环境的不同&#xff0c;配置在不同的properties中&#xff0c;然后从里面进行读取。而Properties类作为最基础也是最经常使用的类&#xff0c;通过本文我们来学习一下它的…

Java中Collections.singletonList用法

Collections.singletonList()返回的是不可变的集合&#xff0c;但是这个长度的集合只有1&#xff0c;可以减少内存空间。但是返回的值依然是Collections的内部实现类&#xff0c;同样没有add的方法&#xff0c;调用add&#xff0c;set方法会报错 调用add方法报错 Exception in…

最近找工作的面试经历

来到广州已经一个星期了&#xff0c;招聘会参加了两场&#xff0c;面试了三间公司&#xff0c;但都是通知回去等结果。回想一下这几天的面试经历&#xff0c;感觉自己要学的东西还很多。 <?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office&quo…

CF Round410 D. Mike and distribution

D. Mike and distribution 构造法 798D - Mike and distribution In the beginning, its quite easy to notice that the condition " 2(ap1  ...  apk) is greater than the sum of all elements in A " is equivalent to " ap1  ...  apk is greater …

OOAD实践之路——真实案例解析OO理论与实践(二、第一项任务:特性列表)

查看本系列全部文章&#xff1a;《OOA&D实践之路——真实案例解析OO理论与实践》索引贴 第一份说明 当这个项目开始时&#xff0c;我们得到的关于我们要做的系统的唯一说明是一页Word文档&#xff0c;这是一份简单的不能再简单的说明。文档里只有一行字&#xff1a;我…