今天就來說說selenium的八種定位方式,八種定位方式基本上可以解決我們自動化測試中定位的絕大部分問題,當然,如果出現定位不到或者元素屬性不可見,這就需要我們瞭解更多的前端知識,通過操作Frame,Dom或者JavaScript來解決。
那今天我們就先來看看有哪八種基本的定位方式呢?
1.通過-id來定位元素,我們以百度爲例,通過-id來定位他的搜索框,利用fireBug我們可以看到他的id="kw",所以我們可以通過代碼來看一下。
public class EightMthods {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
/*
*1. 根絕id來定位元素
*/
String Url = "https://www.baidu.com";
driver.get(Url);
//輸入框
driver.findElement(By.id("kw")).sendKeys("you are beautiful");
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
//百度一下按鈕
driver.findElement(By.id("su")).click();
大家可以運行一下,看看效果吧,但是前提是你的selenium版本要和FireFox或者你的Chrome版本匹配哦
2.通過-xpath來定位,我們還是以百度首頁來舉個栗子。。。
通過F12,我們可以看到輸入框的位置 xpath = ".//*[@id='kw']" ,今天這裏先不說xpath的構造,之後我會專門寫一篇相關的文章介紹,xpath還有一個很好的功能就是他可以debug,比如我們在xpath定位欄輸入 xpath = .//*[@class='s_ipt'] ,然後點回車,這個時候如果這個xpath是有效的,那麼他就會根據xpath路徑去定位到相關的元素,如下圖
如果xpath格式錯誤,那麼他會是顯示紅色的,如下圖
如果xpath路徑錯誤,那麼他會找不到頁面元素的信息
ok,那我們來看一下代碼如何實現的
public class EightMthods {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
/*
*1. 根絕xpath來定位元素
*/
String PhotoUrl = "https://www.baidu.com";
driver.get(PhotoUrl);
//通過xpath定位
driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("Lions");
driver.findElement(By.xpath(".//*[@id='su']")).click();
下面我在介紹幾個xpath的其他寫法
/*
* xpath--text()方法,百度首頁的新聞鏈接
*/
driver.findElement(By.xpath(".//*[@id='u_sp']/a[text()='新聞']")).click();
/*
* 對於動態的元素定位,可以使用contains()
*/
//eg 百度首頁的【新聞元素】
driver.findElement(By.xpath(".//*/a[contains(@href,'news')]")).click();
/*
* xpath相對路徑的寫法
* URL=http://news.baidu.com/
* 先根據for = newstitle 或者text()=新聞標題來定位“新聞標題”這個標籤。
*/
//使用for屬性來定位
driver.findElement(By.xpath(".//*/label[@for='newstitle']/../input[@id='newstitle']")).click();
//使用text來定位
driver.findElement(By.xpath(".//*/label[text()='新聞標題']/../input[@id='newstitle']")).click();
3.PartialLinkText方法定位,基於LinkText,不想寫那麼多的字的時候,選取其中的一部分字段,但是必須在該頁面有唯一性, 一般我會選擇使用LinkText()方法
public class EightMthods {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
/*
*1. 根絕linkText&partialLinkText來定位元素
*/
String PhotoUrl = "http://photo.163.com";
driver.get(PhotoUrl);
//通過xpath定位
/*
*使用下面這段代碼請把地址更換爲百度首頁地址
*driver.findElement(By.linkText("把百度設爲主頁")).click();
*/
WebElement LearnMoreLink = driver.findElement(By.partialLinkText("瞭解更多"));
//點擊瞭解更多
LearnMoreLink.click();
4.用cssSelector 選擇器定位元素,用#id的值表示這個元素的css表達式
public class EightMthods {
public static void main(String[] args) throws Exception {
//首先要創建一個driver驅動的實例對象,這裏用Firefox瀏覽器來演示
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
/*
*1. 根絕cssSelector來定位元素
*/
String PhotoUrl = "https://www.baidu.com";
driver.get(PhotoUrl);
driver.findElement(By.cssSelector("#kw")).sendKeys("For beauty");
driver.findElement(By.cssSelector("#su")).click();
如果感覺自己寫的不準確的話,可以使用Firepath,切換爲CSS選擇器就ok了
吃飯時間到了,今天就先寫到這裏,另外幾種方式我下次再寫。。。。。。
very hungry,
very hungry