WEB自动化(JAVA版)——元素定位(id、name、tagName、className、linkText & partialLinkText)

基本元素定位

在这里插入图片描述

代码示例

进行调试的时候,逐个将定位方法的注释放开即可。

package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

public class ElementLocate {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();
		Thread.sleep(1000);
		//1.定位百度的搜索框元素,并且输入数据(ID定位)--唯一的
		//chromeDriver.findElement(By.id("kw")).sendKeys("自动化测试");
		
		//2.定位百度的搜索框元素,并且输入数据(Name定位)--重复
		//chromeDriver.findElement(By.name("wd")).sendKeys("自动化测试");
		
		//3.定位百度的搜索框元素,并且输入数据(tagName定位)--找到的元素是会有多个--不推荐
		//chromeDriver.findElement(By.tagName("input")).sendKeys("自动化测试");
		
		//4.定位百度的搜索框元素,并且输入数据(className定位)
		//chromeDriver.findElement(By.className("s_ipt")).sendKeys("自动化测试");
		//Compound class names not permitted -->复合类名的问题
		//chromeDriver.findElement(By.className("bg s_btn")).click(); //不可以用这样的复合类名
		//chromeDriver.findElement(By.className("s_btn")).click();
		
		//5.定位“新闻”元素,并且点击(LinkText定位)-->超链接完整文本
		//chromeDriver.findElement(By.linkText("新闻")).click();
		
		//6.定位“新闻”元素,并且点击(partialLinkText定位)-->超链接部分文本
		chromeDriver.findElement(By.partialLinkText("闻")).click();
		
	}
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打开Chrome浏览器
		chromeDriver = new ChromeDriver();
		//2.访问百度
		chromeDriver.get("http://www.baidu.com");
	}
}

备注:
已登录用户,若退出,className属性值会减少。

遇到的问题汇总

问题:className会存在复合类名的问题

	chromeDriver.findElement(By.className("s_ipt")).sendKeys("自动化测试");
	//Compound class names not permitted -->复合类名的问题
	chromeDriver.findElement(By.className("bg s_btn")).click(); //不可以用这样的复合类名
	chromeDriver.findElement(By.className("s_btn")).click(); //运行正常

className复合类名的问题,报错如下:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".bg\ s_btn"}
  (Session info: chrome=80.0.3987.149)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'CNSHA1N6084', ip: '192.168.0.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_241'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 80.0.3987.149, chrome: {chromedriverVersion: 80.0.3987.106 (f68069574609..., userDataDir: C:\Users\chenxi20\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63084}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5a59a5a9af9bc38082b3d39b2e6f08c4
*** Element info: {Using=class name, value=bg s_btn}
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
	at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
	at org.openqa.selenium.remote.RemoteWebDriver.findElementByClassName(RemoteWebDriver.java:412)
	at org.openqa.selenium.By$ByClassName.findElement(By.java:389)
	at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
	at com.adidas.ElementLocate.main(ElementLocate.java:22)

发布了64 篇原创文章 · 获赞 2 · 访问量 2763

猜你喜欢

转载自blog.csdn.net/anniewhite/article/details/105187180