一、测试代码

@Test

public void test() {

WebDriver driver = new FirefoxDriver();

// 打开当前包中的index页面

driver.get("file:///D:/%E8%B5%B5%E6%AC%A2/Selenium/Selenium/src/com/html/index.html");

WaitSeconds(1000);

// 清除用户输入

driver.findElement(By.id("fname")).clear();

WaitSeconds(1000);

// 输入

driver.findElement(By.id("fname")).sendKeys("这是输入的内容");

WaitSeconds(1000);

// 获取元素内容

String text = driver.findElement(By.name("jsh")).getText();// 这种方法是获取元素的文本值

System.out.println("获取元素内容" + text);

WaitSeconds(1000);

// 以下这种方式是获取input的value值

text = driver.findElement(By.id("fname")).getAttribute("value");

System.out.println("value获取元素内容" + text);

WaitSeconds(1000);

// 单击操作

driver.findElement(By.id("idOfButton")).click();

WaitSeconds(1000);

// 通过浏览器记录向后导航

driver.findElement(By.linkText("This is a link")).click();

driver.navigate().back();

WaitSeconds(1000);

// 向前导航

driver.navigate().forward();

// 刷新

driver.navigate().refresh();

// 关闭网页当前页面

driver.close();

// 关闭浏览器,如果有其他选项卡一并关闭

driver.quit();

// 在windows间移动

WaitSeconds(3000);

driver.switchTo().window("MsgWindow");

// 拖拽,首先拖拽的对象要实现拖拽的方法

WaitSeconds(3000);

WebElement source=driver.findElement(By.id("sourceImage"));

WebElement target=driver.findElement(By.id("targetDiv"));

Actions actions=new Actions(driver);

actions.dragAndDrop(source, target).build().perform();

//actions.clickAndHold(source).moveToElement(target).perform();

// 通过标签

driver.findElement(By.tagName("input")).sendKeys("aaaaa");

// 通过linktext

driver.findElement(By.linkText("This is a link")).click();

driver.findElement(By.partialLinkText("This is")).click();

// 处理下拉列表,找到元素之后new一个select对象

WebElement dElement=driver.findElement(By.id("testingDropdown"));

Select dropdownlist=new Select(dElement);

dropdownlist.selectByIndex(3);

WaitSeconds(2000);

dropdownlist.selectByValue("Performance");

WaitSeconds(2000);

dropdownlist.selectByVisibleText("Manual Testing");

WaitSeconds(2000);

dropdownlist.deselectByIndex(2);

// Alert

driver.findElement(By.id("Alert")).click();

WaitSeconds(2000);

driver.switchTo().alert().accept();

WaitSeconds(2000);

driver.findElement(By.id("Confirm")).click();

WaitSeconds(2000);

String txt=driver.switchTo().alert().getText();

System.out.println(txt);

WaitSeconds(2000);

driver.switchTo().alert().dismiss();

// 滚动,需要调用js方法scrollBy(x,y)

JavascriptExecutor javascriptExecutor=(JavascriptExecutor)driver;

javascriptExecutor.executeScript("scrollBy(0,4000)");

}

二、以上测试代码针对的html页面

<html>

<head>

<title>简单测试页面</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="src/com/html/bootstrap.min.css">

<script src="src/com/html/jquery-3.3.1.min.js"></script>

<script src="src/com/html/bootstrap.min.js"></script>

<style></style>

</head>

<body style="font-family: cursive;">

<div class="container">

<div class="row">

<div class="col-md-offset-2 col-md-8" style="font-size: 30; margin-top: 40px; ">

用于自动化测试的Web页面示例

</div>

</div>

<div class="row">

<div class="col-md-12" style="font-size:20px; margin-top:40px;" name="jsh">

This is sample webpage with dummy elements that will help you in learning selenium automation.

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<b>This is sample text.</b>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p> <b>Link : </b><a href="https://www.yiibai.com/">This is a link</a></p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>TextBox : </b><input id="fname" type="text" name="firstName" value="文本框内容" ><input id="fname2" type="text" name="firstName" value="第二个文本框" ></p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Button : </b><button id="idOfButton" title="Click me!!" type="button" οnclick="this.style.background='green';">Submit</button></p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Radio button : </b>

<form action="#">

<input id="male" type="radio" name="gender" value="male"> Male

<input id="female" type="radio" name="gender" value="female"> Female

</form>

</p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Checkbox :</b>

<form action="#">

<input type="checkbox" class="Automation" value="Automation"> Automation Testing

<input type="checkbox" class="Performance" value="Performance"> Performance Testing

</form>

</p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Drop down :</b>

<select id="testingDropdown">

<option id="automation" value="Automation">Automation Testing</option>

<option id="performance" value="Performance">Performance Testing</option>

<option id="manual" value="Manual">Manual Testing</option>

<option id="database" value="Database">Database Testing</option>

</select>

</p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><button id="dblClkBtn" οndblclick="alert('hi, Yiibai Testing');">Double-click to generate alert box</button></p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Click button to generate Alert box : </b>

<button id="Alert" οnclick="alert('hi, Yiibai Testing');">Generate Alert Box</button>

</p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p> <b> Click button to generate Confirm box : </b>

<button id="confirm" οnclick="generateConfirmBox()">Generate Confirm Box</button>

</p>

<p id="demo"></p>

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p>Drag and drop example- drag the below image on the textbox</p>

<div id="targetDiv" οndrοp="drop(event)" οndragοver="allowDrop(event)" style="width:400px;height:150px;padding:10px;border:1px solid #aaaaaa;"></div>

<img id="sourceImage" src="https://www.yiibai.com/static/img/logo.png" alt="yiibai" draggable="true" οndragstart="drag(event)" height="120px">

</div>

</div>

<br>

</div>

<script>

//window.open('http://www.baidu.com','MsgWindow');

function generateConfirmBox()

{

var x;

var r=confirm("Press a button!");

if (r==true)

{

x="You pressed OK!";

}

else

{

x="You pressed Cancel!";

}

document.getElementById("demo").innerHTML=x;

}

function allowDrop(ev)

{

ev.preventDefault();

}

function drag(ev)

{

ev.dataTransfer.setData("Text",ev.target.id);

}

function drop(ev)

{

ev.preventDefault();

var data=ev.dataTransfer.getData("Text");

ev.target.appendChild(document.getElementById(data));

}

</script>

</body>

</html>

转载于:https://www.cnblogs.com/zh1990/p/10648969.html

Selenium-基础操作相关推荐

  1. Python Selenium 基础入门

      本内容主要介绍 Python Selenium 的基础使用方法. 1 Python Selenium 简介和环境配置 1.1 Selenium 简介   Selenium 是一个 Web 的自动化 ...

  2. [Python从零到壹] 九.网络爬虫之Selenium基础技术万字详解(定位元素、常用方法、键盘鼠标操作)

    欢迎大家来到"Python从零到壹",在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界.所有文章都将结合案例.代码和作者的经验讲 ...

  3. python模拟浏览器下载文件在哪里_python下selenium模拟浏览器基础操作

    1.安装及下载 selenium安装: pip install selenium  即可自动安装selenium geckodriver下载:https://github.com/mozilla/ge ...

  4. Selenium基础 — Selenium操作浏览器窗口滚动条

    1.为什么操作滚动条 在HTML页面中,由于前端技术框架的原因,页面中的一些元素为动态显示,元素根据滚动条的下拉而被加载. 例如:页面注册同意条款,需要滚动条到最底层,才能点击同意. 2.Seleni ...

  5. 提交表单自动刷新_Web自动化测试:元素的基础操作和浏览器基础操作

    上一节,我们了解了如何定位元素,其实也有涉及对于元素的操作,这一节我们就详细的介绍一下对于元素的操作和对于浏览器的一些操作 一.对于元素的基础操作: clear():清除输入框内的文本 send_ke ...

  6. web自动化如何在不同浏览器运行_Web自动化测试:元素的基础操作和浏览器基础操作...

    上一节,我们了解了如何定位元素,其实也有涉及对于元素的操作,这一节我们就详细的介绍一下对于元素的操作和对于浏览器的一些操作 一.对于元素的基础操作: clear():清除输入框内的文本 send_ke ...

  7. selenium鼠标操作 包含右击和浮层菜单的选择

    感谢http://www.cnblogs.com/tobecrazy/p/3969390.html  博友的分享 最近在学习selenium的一些鼠标的相关操作 自己在百度的相关操作代码 /** * ...

  8. web 自动化测试 selenium基础到应用(目录)

    第一章   自动化测试前提及整体介绍 1-1功能测试和自动化测试的区别 1-2自动化测试流程有哪些 1-3自动化测试用例和手工用例的区别 1-4 自动化测试用例编写 1-5 selenium的优势以及 ...

  9. Selenium基础用法

    目录 一.概念和自己的理解 二.安装 三. 浏览器驱动 四.正真的基础上场 1.先要打开浏览器,打不开,我们后面也就做不了,万事开头先有前提 2.获取元素的方法 3.操作元素 4.浏览器操作 5.鼠标 ...

  10. 自动化爬虫selenium基础教程

    一.前期准备 二.基础操作 1.实例化一个浏览器对象 2.对url发起请求 3.标签定位 4.标签交互 5.点击按钮 6.回退.前进和关闭 7.解析数据 8.执行JavaScript程序 9.实现无可 ...

最新文章

  1. Ceph 的用户管理与认证
  2. Fisher线性判别算法原理及实现 MATLAB
  3. spring boot aop 记录方法执行时间
  4. ubuntu 如何用root身份进行登录
  5. 漫谈C++:良好的编程习惯与编程要点
  6. 【重磅资料】ArchSummit全球架构师峰会·2019华为云技术专场资料下载
  7. MATLAB矩阵的算术运算
  8. ASP.NET-第八天-加强课程
  9. 激情彭拜的10月英语学习
  10. ELI'S CURIOUS MIND
  11. “D语言风采不再”的说法言过其实
  12. 写在2014年的感恩节
  13. 【PMP】专题沟通管理 错题
  14. 【LeetCode343】剪绳子(动态规划)
  15. 判断移动端PC端访问网页时跳转到对应的移动端网页
  16. (转载)常见的程序员健康问题
  17. 高光谱图像基础知识(一)
  18. 1.#INF、-1.#INF、1.#IND、-1.#IND 问题
  19. HTML转换为WORD
  20. 硅谷钢铁侠-读书笔记

热门文章

  1. JAVA面试题------------final 关键字是干什么用的?谈谈你的理解。
  2. dpkg ihr状态_考勤机数据无法同步
  3. 收集6 款 Java 8 自带工具,轻松分析定位 JVM 性能问题!
  4. 分布式数据库管理系统介绍
  5. 国企程序员是一种怎样的体验?
  6. MYSQL中表级锁、行级锁、页级锁介绍
  7. 几款开源富文本编辑器优缺点比较
  8. SQLl中的left join、right join、inner join详解
  9. bmp 像素点 php,读取BMP图像每一像素点RGB数据 | 学步园
  10. 1 睡眠唤醒_一劳永逸解决WIN10所有睡眠问题