在第一节中,我们已经成功打开了页面,但是自动化测试必然包含了表单的填写与按钮的点击. 所以在第二章中我以博客园为例,完成按钮点击,表单填写

还是以代码为准,先上代码:

package com.ryan;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", ".\\tools\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.cnblogs.com/ryan255/");
System.out.println(driver.getTitle());      //获取页面上的按钮
WebElement button = driver.findElement(By.cssSelector("#blog_nav_admin"));
     //点击按钮
button.click();
     //获取页面上的输入框  
WebElement input1 = driver.findElement(By.cssSelector("#input1"));
     //清理输入框的内容
input1.clear();
     //输入
input1.sendKeys("ryan255");
     Thread.sleep(5000); 
driver.quit();
}
}

如上面的注释所示这里的代码增加了获取页面元素、点击按钮、输入表单的操作。

Selenium提供了8种定位方式:
  • id
  • name
  • class name
  • tag name
  • link text
  • partial link text
  • xpath
  • css selector
这8种定位方式在代码中所对应的方法为:
  • findElement(By.id())
  • findElement(By.name())
  • findElement(By.className())
  • findElement(By.tagName())
  • findElement(By.linkText())
  • findElement(By.partialLinkText())
  • findElement(By.xpath())
  • findElement(By.cssSelector())

对于chrome来说,有一个非常方便的定位页面元素的方法:

1. F12进入调试页面,点击小箭头,再点击你想要获取的页面元素(如本例中获取的管理按钮)

2.在所选元素的html上右键, Copy -> Copy selector或者 Copy XPath, 元素就被定位了.

selenium java maven 自动化测试(二) 页面元素获取与操作的更多相关文章

  1. selenium java maven 自动化测试(一) helloworld

    本教程使用selenium-java,简单的完成了网页访问 网页内容获取,表单填写以及按钮点击. 1. 使用maven构建项目 在pom中添加如下依赖: <dependency> < ...

  2. selenium java maven自动化测试环境搭建

    版本说明: JDK 版本:1.8.0_112: Eclipse IDE: 4.6.1: Maven 版本:apache-maven-3.3.9: Selenium 版本: 3.0.1: Firefox ...

  3. Java&Selenium自动化测试实现页面元素、页面对象及测试代码分离

    一.摘要 本篇博文将介绍自动化测试实现页面元素.页面对象及测试代码分离在自动化框架中的实现 二.解析页面元素定位信息 首先,将页面元素与实际的代码分离,首先我们将页面元素定位信息和定位表达式保存在属性 ...

  4. Python3.x:Selenium中的webdriver进行页面元素定位

    Python3.x:Selenium中的webdriver进行页面元素定位 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素属性(class属性,name属性)等等.webdriver ...

  5. selenium java maven testNg环境搭建

    maven获取jar的xml地址:http://mvnrepository.com 步骤一安装jdk(略) 步骤二 安装eclipse(略) 步骤三 安装testNG 步骤四 maven安装 步骤三 ...

  6. web自动化测试---web页面元素的定位

    selenium提供了很多用于定位元素的方法,首先我们自己需要知道元素有哪些属性,这就需要用到安装测试环境中firebug来定位 打开firefox浏览器,按下F12键,我们就可以看到如下图所示的界面 ...

  7. Selenium WebDriver-通过ActionChains实现页面元素拖拽

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  8. selenium—用NoSuchElementException异常判断页面元素是否存在

    一.知识补充 1.find_element的一种使用方法: find_element(by=方法,value=值) 例如: find_element(by="id",value=& ...

  9. Selenium WebDriver- 操作frame中的页面元素

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

随机推荐

  1. react context toggleButton demo

    //toggleButton demo: //code: //1.Appb.js: import React from 'react'; import {ThemeContext, themes} f ...

  2. php判断网站收录情况

    php判断网站收录情况 <?php //检测网页是否被百度收录 function checkBaiduIndex($url){ $url='http://www.baidu.com/s?wd=' ...

  3. [2014年学习计划之RoR系列] 第二步 – 熟悉Ruby语言 (2/n) Blocks and Iterators (代码块和迭代器)

    [就算没有含金量,也请尊重原创, 转载自我的独立博客http://brucejia.net] Blocks and Iterators (代码块和迭代器) 代码块和迭代器是Ruby语言中比较有特点的东 ...

  4. 算法之水仙花数(Java语言)

    概述 在数论中,水仙花数(Narcissistic number),也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯 ...

  5. 3.Bootstrap CSS 概述

    1.HTML 5 文档类型(Doctype) Bootstrap 使用了一些 HTML5 元素和 CSS 属性.为了让这些正常工作,您需要使用 HTML5 文档类型(Doctype). 因此,请在使用 ...

  6. Docker Toolbox更换镜像源并下载centos7

  7. 三年Linux运维工作总结教训

    Linux运维一定要知道的六类好习惯和23个教训,避免入坑! 从事运维三年半,遇到过各式各样的问题,数据丢失,网站挂马,误删数据库文件,黑客攻击等各类问题. 今天简单整理一下,分享给各位小伙伴. 一. ...

  8. 在IIS上发布网站后,在编译时出现CS0016拒绝访问错误

    错误如下图所示:     关键性错误信息:   编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Te ...

  9. 最新php环境搭建,2017年最新PHP环境搭建

    使用PHPWAMP8.8.8.8IN纯绿色集成环境搭建php网站很方便,哪怕你系统没安装vc.系统dll丢失或者损坏的时候,phpwamp也可以正常启动,是个很强大的php套件. php环境搭建,支持 ...

  10. git操作:

    error: Your local changes to the following files would be overwritten by merge: **/**/**.php Please, ...