selenium webdriver学习(一)

 

复制代码
 package baidu;

 

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

 

public class selenium  {
    

 
    public static void main (String [] args) throws InterruptedException
    {
         
     
        String URL="http://www.baidu.com";
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe"); 
        WebDriver driver = new ChromeDriver();
        driver.get(URL);
         
 /*
        Navigation navigation = driver.navigate();
         navigation.to(URL);*/
         Thread.sleep(2000);
         
         //WebElement reg=driver.findElement(By.name("tj_reg"));
         //reg.click();
     //    WebElement keyWord = driver.findElement(By.id("kw1"));
         WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']"));
         
        keyWord.clear();
         keyWord.sendKeys("Selenium");
         Thread.sleep(3000);
         
 
         
          WebElement submit = driver.findElement(By.id("su1"));
          
          System.out.println(submit.getLocation());
          submit.click();
          System.out.println(driver.getWindowHandle());
         Thread.sleep(5000);
           File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            // Now you can do whatever you need to do with it, for example copy somewhere
            try {
                FileUtils.copyFile(scrFile, new File("E:\\screenshot.png"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        // System.out.println(driver.getPageSource());
        String pageSource=driver.getPageSource();
        System.out.println(pageSource);
        WebElement webElement =driver.findElement(By.xpath("/html"));
        if(pageSource.matches("http://www.baidu.com/link?"))
        {
             System.out.println("*************PASS***********");
        }
        else
        {
            System.out.println("*************FAIL***********");
        }
        System.out.println(webElement.getText());
        System.out.println(driver.getTitle());
         Thread.sleep(5000);
    //     navigation.back();
          
         System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());
         
     
         
          driver.quit();
           
       
    }
    
}
View Code
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package http;
 
import org.openqa.selenium.Alert;
 
import org.openqa.selenium.JavascriptExecutor;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
 
public class selenium {
 
    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
 
        String URL="http://www.baidu.com";
        System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
        DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
        ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
        WebDriver driver = new InternetExplorerDriver(ieCapabilities)   ;
        driver.manage().window().maximize();
         
         
         
       driver.get(URL);
  
       /* Navigation navigation = driver.navigate();
         navigation.to(URL); */
       
      // Alert a=  driver.switchTo().alert();
     //  a.accept();
         Thread.sleep(2000);
     //    WebElement keyWord = driver.findElement(By.id("kw1"));
          
         
         WebElement keyWord = driver.findElement(By.id("kw1"));
          
        // WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw']"));
          
          
         WebElement f=driver.findElement(By.name("f"));
          
         System.out.println(f.getText());
         if(keyWord.isDisplayed())
         {
             keyWord.sendKeys("Selenium");
         }
         else
              
         {
             System.out.print("can't fund\n");
         }
          
         ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\"");
 
          
        // Thread.sleep(3000);
          
         Alert alert=driver.switchTo().alert();
         System.out.println(alert.getText());
          
         alert.dismiss();
          
         WebElement submit = driver.findElement(By.id("su1"));
         Thread.sleep(2000);
         if(submit.isDisplayed())
         {  
             submit.click();
         }
         else
         {
             driver.quit();
         }
         Thread.sleep(5000);
          System.out.println(driver.getTitle());
         
    //     navigation.back();
         
         Thread.sleep(5000);
       //  System.out.println(driver.getPageSource());
         System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());
          driver.quit();
    }
 
}

 

posted @   to be crazy  阅读(3278)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示