Selenium元素定位

//XPath
//firebug-firepath-XPath-Highlight框选
//
driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("xpath-selenium");
//CSS
//firebug-firepath-CSS-Highlight框选
//
//如:css、jQuery , id前面有#
//#id //.class
driver.findElement(By.cssSelector("#kw")).sendKeys("CSS-cssSelector-selenium");
//延时5s
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//
//id="kw"
driver.findElement(By.id("kw")).sendKeys("id-selenium");

driver.findElement(By.id("kw")).clear();
//name="wd"
driver.findElement(By.name("wd")).sendKeys("name-selenium");
//class="s_ipt"
driver.findElement(By.className("s_ipt")).sendKeys("class-selenium");
//
driver.findElement(By.tagName("input")).sendKeys("tagName-标签名-input-selenium");
//新闻
//a标签的text
driver.findElement(By.linkText("新闻")).click();
//a标签的部分text
driver.findElement(By.partialLinkText("新")).click();
package com.selenium;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class localTest {

    public static WebDriver driver;
    
    public static void main(String[] args) {
        
        // TODO Auto-generated method stub
        System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
        driver = new FirefoxDriver();
        
        String testUrl = "http://baidu.com";
        driver.get(testUrl);
        
        //basicElement();
        
        //获取元素类型
        System.out.print(driver.findElement(By.tagName("input")).getClass());
        
        //findElements
        List inputs = driver.findElements(By.tagName("input")); 
        System.out.print(inputs.size());//size
        
        for (WebElement input : inputs) {
            System.out.print("111");
//          String value = input.getText();
//          System.out.print(value+"\n");
        }
        
    }
    
    public static void basicElement(){
        
        //XPath
        //firebug-firepath-XPath-Highlight框选
        //
        driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("xpath-selenium");
        
        //CSS
        //firebug-firepath-CSS-Highlight框选
        //
        //如:css、jQuery , id前面有#
        //#id //.class
        driver.findElement(By.cssSelector("#kw")).sendKeys("CSS-cssSelector-selenium");

        //延时5s
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        
        //
        //id="kw"
        driver.findElement(By.id("kw")).sendKeys("id-selenium");
        driver.findElement(By.id("kw")).clear();
        
        //name="wd"
        driver.findElement(By.name("wd")).sendKeys("name-selenium");
        driver.findElement(By.name("wd")).clear();
        
        //class="s_ipt"
        driver.findElement(By.className("s_ipt")).sendKeys("class-selenium");
        driver.findElement(By.className("s_ipt")).clear();
        
        //
        driver.findElement(By.tagName("input")).sendKeys("tagName-标签名-input-selenium");
        driver.findElement(By.tagName("input")).clear();
        
        //新闻
        //a标签的text
        driver.findElement(By.linkText("新闻")).click();
        //a标签的部分text
        driver.findElement(By.partialLinkText("新")).click();
    }

}

你可能感兴趣的:(Selenium元素定位)