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.test.ElementLocate.main(ElementLocate.java:22)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章