Selenium drives each browser code under the Java programming language

The Selenium 3.7 version is used here, and it is first introduced to run in the Windows environment;

To summarize the notes:

1. Set the driver path of each browser

System.setProperty("","");

2. Create a browser object

WebDriver driver = new xxx();

 

 

1. Drive IE browser

There are two versions of IE browser driver, 32-bit and 64-bit. It is recommended to use 32-bit IEdriver, because the 64-bit IE driver is too slow to run.

 1 package base;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.ie.InternetExplorerDriver;
 6 
 7 public class TestIEDriver {
 8 
 9     public static void main(String args[]) throws InterruptedException {
10 
11         System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
12         WebDriver driver = new InternetExplorerDriver();
13         driver.manage().window().maximize();
14         driver.get("http://www.baidu.com");
15         String s = driver.getTitle();
16         System.out.print(s);
17         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
18         Thread.sleep(1000);
19         driver.close();
20 
21     }
22 }

 

2. Drive Chrome browser

 1 package base;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.chrome.ChromeDriver;
 8 import org.openqa.selenium.chrome.ChromeOptions;
 9 import org.openqa.selenium.remote.DesiredCapabilities;
10 
11 public class TestChromeDriver {
12 
13     public static void main(String args[]) throws InterruptedException {
14 
15         System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
16         ChromeOptions options = new ChromeOptions();
17         DesiredCapabilities capabilities = DesiredCapabilities.chrome();
18         capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
19         options.addArguments("--test-type", "--start-maximized");
20         WebDriver driver = new ChromeDriver(options);
21         driver.get("http://www.baidu.com");
22 
23         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
24         Thread.sleep(1000);
25         driver.close();
26 
27     }
28 
29 }

 

3. Drive Firefox FireFox browser

 1 package base;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.openqa.selenium.WebDriver;
 6 import org.openqa.selenium.firefox.FirefoxDriver;
 7 
 8 public class TestFireFoxDriver {
 9 
10     public static void main(String args[]) throws InterruptedException {
11 
12         System.setProperty("webdriver.Firefox.driver", ".\\Tools\\geckodriver.exe");
13         WebDriver driver = new FirefoxDriver();
14         driver.manage().window().maximize();
15         driver.get("http://www.baidu.com");
16         String s = driver.getTitle();
17         System.out.print(s);
18         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19         Thread.sleep(1000);
20         driver.close();
21 
22     }
23 
24 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325165805&siteId=291194637