一、测试代码

@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" onclick="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" ondblclick="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" onclick="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" onclick="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" ondrop="drop(event)" ondragover="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" ondragstart="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>

Selenium-基础操作的更多相关文章

  1. selenium基础操作

    selenium 1.打开和关闭网页 #!/usr/bin/env python # -*- coding:utf-8 -*- from selenium import webdriver drive ...

  2. <selenium>selenium基础操作

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...

  3. python下selenium模拟浏览器基础操作

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

  4. selenium基础(下拉菜单操作)

    selenium基础(下拉菜单操作) 非select/option元素: 1.触发下拉列表出现 2.等待下拉列表中的元素出现,然后进行选择元素即可. select/option元素: 下拉框操作-Se ...

  5. selenium 基础(一)

    selenium安装 pip install selenium selenium操作浏览器原理 早期selenium 1.0 用的selenium RC, 后来selenum2集合了selenium1 ...

  6. 【Python爬虫】selenium基础用法

    selenium 基础用法 阅读目录 初识selenium 基本使用 查找元素 元素互交操作 执行JavaScript 获取元素信息 等待 前进后退 Cookies 选项卡管理 异常处理 初识sele ...

  7. Selenium | 基础入门 | 截屏并保存于本地

    可先参考   Selenium | 基础入门 | 利用Xpath寻找用户框 核心代码: //截屏操作 File srcFile = ((TakesScreenshot)driver).getScree ...

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

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

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

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

  10. selenium基础-跳过验证码

    selenium基础-跳过验证码 一.方法 设置万能验证码或者屏蔽验证码(最常用的方法) 使用验证码识别工具识别验证码 通过selenium操作cookies 直接使用配置文件的webdriver 二 ...

随机推荐

  1. spring mvc 解决csrf跨站请求攻击

    http://www.dewen.net.cn/q/935/spring+mvc+%E8%A7%A3%E5%86%B3csrf%E8%B7%A8%E7%AB%99%E8%AF%B7%E6%B1%82% ...

  2. ios开发处理服务器返回的时间字符串

    #import <Foundation/Foundation.h> void other(); void string2date(); int main(int argc, const c ...

  3. js动态获取地址栏后的参数

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78866651 需求:js动态的获取地址栏后面的参数 js代码: alert(GetQu ...

  4. [iOS]iOS开发人员账号申请与注冊流程

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  5. matplotlib 可视化 —— cmap(colormap)

    color example code: colormaps_reference.py - Matplotlib 2.0.0 documentation 由其文档可知,在 colormap 类别上,有如 ...

  6. js获取input file路径改变图像地址

    版权声明:好歹是我写的或者总结的或者抄的,总待给我个名份吧~ https://blog.csdn.net/sangjinchao/article/details/52250318 html代码 < ...

  7. 【Unity Shaders】Lighting Models —— 灯型号Lit Sphere

    考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同一时候会加上一点个人理解或拓展. 这里是本书全部的插图.这里是本书所需的代码和资源(当然你 ...

  8. [GeekBand] 设计模式——工厂模式学习笔记

     本文参考文献:GeekBand课堂内容,授课老师:李建忠 :大话设计模式 其余的模式方法请自行查看Geekband相关课程,在此不累述. 这周的课题是: 针对DrawingSystem中的基类Sha ...

  9. 《80x86汇编语言程序设计》保护模式第一个例题

    <80x86汇编语言程序设计>保护模式第一个例题的一些个人理解和注视 ; 16位偏移的段间直接转移指令的宏定义 jump macro selector, offsetv db 0eah   ...

  10. JDK里的设计模式(全)

    下面是JDK中有关23个经典设计模式的示例,在stakeoverflow也有相应的讨论:http://stackoverflow.com/questions/1673841/examples-of-g ...