1. 程式人生 > >selenium + java 字串擷取

selenium + java 字串擷取

public class StringOpration {


    public static void main(String[] args) throws InterruptedException {

        //驅動設定
        System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver ();
        driver.manage ().window ().maximize ();
        driver.get ( "http://www.baidu.com" );
      
        //sendKeys鍵盤輸入“selenium” and 點選“百度一下” 按鈕
        driver.findElement ( By.id ( "kw") ).sendKeys ( "selenium" );
        driver.findElement ( By.id ( "su" ) ).click ();
        Thread.sleep ( 2000 );
        
        //定位約.....個元素
        WebElement res=driver.findElement ( By.xpath ( "//*/div[@class='nums']" ) );
        String res_string =res.getText ();

        //擷取
        String st1 =res_string.split ( "約" )[1];

        String search_number=st1.split ( "個" )[0];
        System.out.println (search_number);


        driver.quit ();

    }
}