Go on:下面拓展一些其他WebDriver的方法: GET系列的方法:getTitle()&&getPageSource() package com.cxy.cn; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ChinaWebDriverTest { private static String url="http://www.bignox.com/"; public static void main(String args[]){ System.setProperty("webdriver.chrome.driver", "C:\\browser\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(url); String title=driver.getTitle(); System.out.println(title); String pagesource = driver.getPageSource(); System.out.println(pagesource); driver.quit(); } } 1.1
String title=driver.getTitle(): 获取当前页面的title属性的值,一般利用这个属性可以判断页面是否跳转成功。 1.2 String pagesource = driver.getPageSource(): 获取当前页面整个页面的源码,可以在后台打印出来查看。 GET系列的方法:getCurrentUrl()&&getAttribute()&&getText() package com.cxy.cn; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ChinaWebDriverTest { private static String url="http://www.bignox.com/"; public static void main(String args[]){ System.setProperty("webdriver.chrome.driver", "C:\\browser\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(url); String targeturl=driver.getCurrentUrl(); System.out.println(targeturl); WebElement element = driver.findElement(By.xpath("//*[@id='index-page']/footer/div[3]/ul/li[2]/a/span")); String aa =
element.getAttribute("id"); String a2 =
element.getText(); System.out.println(aa); System.out.println(a2); driver.quit(); } } WebElement element = driver.findElement(By.xpath("//*[@id='index-page']/footer/div[3]/ul/li[2]/a/span")); 1.先定位到这个元素的位置。 2.利用getattribute这个方法获取这个属性对应的值。 3.getText利用定位到的元素获取其中的文字信息。 |