Selenium元素定位

//XPath
//firebug-firepath-XPath-Highlight框选
//<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/>
driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("xpath-selenium");
//CSS
//firebug-firepath-CSS-Highlight框选
//<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/>
//如:css、jQuery , id前面有#
//#id //.class
driver.findElement(By.cssSelector("#kw")).sendKeys("CSS-cssSelector-selenium");
//延时5s
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd">
//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");
//<input/>
driver.findElement(By.tagName("input")).sendKeys("tagName-标签名-input-selenium");
//<a class="mnav" name="tj_trnews" href="http://news.baidu.com">新闻</a>
//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<WebElement> 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框选
        //<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/>
        driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("xpath-selenium");
        
        //CSS
        //firebug-firepath-CSS-Highlight框选
        //<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/>
        //如:css、jQuery , id前面有#
        //#id //.class
        driver.findElement(By.cssSelector("#kw")).sendKeys("CSS-cssSelector-selenium");

        //延时5s
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        
        //<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd">
        //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();
        
        //<input/>
        driver.findElement(By.tagName("input")).sendKeys("tagName-标签名-input-selenium");
        driver.findElement(By.tagName("input")).clear();
        
        //<a class="mnav" name="tj_trnews" href="http://news.baidu.com">新闻</a>
        //a标签的text
        driver.findElement(By.linkText("新闻")).click();
        //a标签的部分text
        driver.findElement(By.partialLinkText("新")).click();
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容