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" >新聞</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" >新聞</a>
        //a標簽的text
        driver.findElement(By.linkText("新聞")).click();
        //a標簽的部分text
        driver.findElement(By.partialLinkText("新")).click();
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容