1, 通过ID 查找
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.firefox.FirefoxBinary;
-
import org.openqa.selenium.chrome.*;
-
-
/**
-
* test Baidu By Id
-
*
-
*/
-
public class testBaiduById
-
{
-
public static void main( String[] args ) throws InterruptedException
-
{
-
System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
-
-
WebDriver driver = new FirefoxDriver();
-
-
driver.get("");
-
WebElement searchBox = driver.findElement(By.id("kw"));
-
searchBox.sendKeys("test Baidu by Id");
-
-
WebElement searchButton = driver.findElement(By.id("su"));
-
searchButton.submit();
-
-
Thread.sleep(2000);
-
-
driver.close();
-
}
-
}
2, 通过name 查找
-
package seleniumtest.seleniumtest;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
-
public class searchbyname {
-
public static void main( String[] args ) throws InterruptedException
-
{
-
System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
-
-
WebDriver driver = new FirefoxDriver();
-
-
driver.get("");
-
WebElement searchBox = driver.findElement(By.name("q"));
-
searchBox.sendKeys("test Douban by Name");
-
searchBox.submit();
-
-
Thread.sleep(10000);
-
-
driver.close();
-
}
-
}
3, 通过classname查找
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class searchbyclassname {
-
public static void main(String[] args) throws InterruptedException {
-
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
//第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
-
WebDriver driver = new FirefoxDriver();
-
driver.manage().window().maximize();
-
driver.get("");
-
-
WebElement searchBox = driver.findElement(By.className("search-combobox-input"));
-
searchBox.sendKeys("test Taobao by Classname");
-
searchBox.submit();
-
-
Thread.sleep(5000);
-
-
driver.quit();
-
}
-
}
4, 通过tagname 查找
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
import java.util.List;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class searchbytagname {
-
public static void main(String[] args) throws InterruptedException {
-
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
//第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
-
WebDriver driver = new FirefoxDriver();
-
driver.manage().window().maximize();
-
driver.get("");
-
-
List<WebElement> scriptlist = driver.findElements(By.tagName("script"));
-
System.out.println("There are "+scriptlist.size()+" Scripts in Xiaomi’s main page");
-
-
Thread.sleep(5000);
-
-
driver.quit();
-
}
-
}
5, 通过linkText 查找:
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
import java.util.List;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class searchbylinktext {
-
public static void main(String[] args) throws InterruptedException {
-
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
//第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
-
WebDriver driver = new FirefoxDriver();
-
//driver.manage().window().maximize();
-
driver.get("");
-
-
WebElement contactLink = driver.findElement(By.linkText("联系方式"));
-
contactLink.click();
-
-
Thread.sleep(5000);
-
-
driver.quit();
-
}
-
}
6,通过PartialLinkText查找:
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
import java.util.List;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class searchbyPartialLinkText {
-
public static void main(String[] args) throws InterruptedException {
-
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
//第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
-
WebDriver driver = new FirefoxDriver();
-
//driver.manage().window().maximize();
-
driver.get("");
-
-
WebElement contactLink = driver.findElement(By.partialLinkText("联系"));
-
contactLink.click();
-
-
Thread.sleep(5000);
-
-
driver.quit();
-
}
-
}
7,通过CSS selector查找:
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
import java.util.List;
-
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class searchbyCSSselector {
-
public static void main(String[] args) throws InterruptedException {
-
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
//第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
-
WebDriver driver = new FirefoxDriver();
-
//driver.manage().window().maximize();
-
driver.get("");
-
-
WebElement searchBox = driver.findElement(By.cssSelector("#kw"));
-
searchBox.sendKeys("webdriver");
-
searchBox.submit();
-
-
Thread.sleep(5000);
-
-
driver.quit();
-
}
-
}
8, 通过XPath查找:
-
package seleniumtest.seleniumtest;
-
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.testng.annotations.Test;
-
import org.testng.annotations.AfterClass;
-
import org.testng.annotations.BeforeClass;
-
import org.openqa.selenium.*;
-
import org.openqa.selenium.support.ui.Wait;
-
import org.openqa.selenium.support.ui.WebDriverWait;
-
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
-
-
-
public class searchbyXPathTest {
-
-
WebDriver driver;
-
@BeforeClass
-
public void setup() {
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
driver = new FirefoxDriver();
-
}
-
-
@AfterClass
-
public void tearDown() {
-
driver.close();
-
// driver.quit();
-
}
-
-
@Test
-
public void testBaidu() throws InterruptedException {
-
driver.get("");
-
-
WebElement searchBox = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div[1]/div/form/span[1]/input"));
-
searchBox.sendKeys("Selenium");
-
-
WebElement searchButton = driver.findElement(By.xpath("//*[@id='su']"));
-
searchButton.click();
-
-
Wait <WebDriver> wait = new WebDriverWait(driver,30);
-
wait.until(visibilityOfElementLocated(By.xpath("/html/body/div[2]/div[5]/div[1]/div[3]")));
-
-
}
-
}
9, 通过jQuery查找:
(1)处理已经加载jQuery的页面:
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
import java.util.List;
-
-
import static org.testng.Assert.assertEquals;
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class searchbyjQuery {
-
public static void main(String[] args) throws InterruptedException {
-
-
System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
-
//第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
-
WebDriver driver = new FirefoxDriver();
-
-
driver.get("");
-
-
JavascriptExecutor jse = (JavascriptExecutor)driver;
-
-
List <WebElement> elements = (List <WebElement>) jse.executeScript("return jQuery.find('.menu-itm')");
-
-
System.out.println(elements.size());
-
assertEquals(10,elements.size());
-
assertEquals("Members",elements.get(2).getText());
-
-
driver.quit();
-
}
-
}
(2)处理未加载jQuery的页面:
-
package seleniumtest.seleniumtest;
-
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
import java.util.List;
-
-
import org.testng.annotations.*;
-
import static org.testng.AssertJUnit.assertEquals;
-
import org.openqa.selenium.By;
-
import org.openqa.selenium.JavascriptExecutor;
-
import org.openqa.selenium.WebDriver;
-
import org.openqa.selenium.WebDriverException;
-
import org.openqa.selenium.WebElement;
-
import org.openqa.selenium.firefox.FirefoxDriver;
-
import org.openqa.selenium.remote.DesiredCapabilities;
-
import org.openqa.selenium.remote.RemoteWebDriver;
-
-
public class loadjQuery{
-
WebDriver driver;
-
JavascriptExecutor jse;
-
-
@BeforeClass
-
public void setup()
-
{
-
System.setProperty("webdriver.gecko.driver", "C:\\Users\\liutaoxa\\geckodriver.exe");
-
driver = new FirefoxDriver();
-
jse = (JavascriptExecutor)driver;
-
}
-
-
@AfterClass
-
public void tearDown()
-
{
-
driver.close();
-
driver.quit();
-
}
-
-
@Test
-
public void testBaidu() throws InterruptedException
-
{
-
driver.get("");
-
-
//load jQuery dynamically if needed
-
injectjQueryIfNeeded();
-
-
//find the top menu items on baidu page
-
List <WebElement> elements = (List <WebElement>)jse.executeScript("return jQuery.find('li.gbt span.gbts')");
-
-
//verify the count of the menu items
-
assertEquals(0,elements.size());
-
-
//verify the property of element "Images"
-
assertEquals("Images",elements.get(2).getAttribute("textContent"));
-
}
-
-
private void injectjQueryIfNeeded()
-
{
-
if( !jQueryLoaded() )
-
{
-
injectjQuery();
-
}
-
}
-
-
public Boolean jQueryLoaded(){
-
Boolean loaded = true;
-
try{
-
loaded = (Boolean)jse.executeScript("return jQuery()! = null");
-
}catch(WebDriverException e){
-
loaded = false;
-
}
-
return loaded;
-
}
-
-
public void injectjQuery(){
-
//load jQuery dynamically in the head of web page
-
jse.executeScript(
-
"var headID = document.getElementsByTagName(\"head\")[0];" +
-
"var newScript = document.createElement('script');" +
-
"newScript.src = '" +
-
"/jQuery/jquery-1.8.0.js';" +
-
"headID.appendChild(newScript)");
-
}
-
}
阅读(738) | 评论(0) | 转发(0) |