opening chrome in selenium issue

前端 未结 1 1612
野趣味
野趣味 2020-12-21 07:17

I\'m currently taking my first automated test class and the instructor has us creating a program in Eclipse after loading Selenium and create a step in the program to look a

相关标签:
1条回答
  • 2020-12-21 07:49

    I think this is what you want

    This code is to open the default browser and go to a specific link You can specify the path of any browser you want from the path in the code

    import java.awt.Desktop;
    import java.net.URI;
    
    public class openBrowser {
    
        public openBrowser() {
            try {
                if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                    Desktop.getDesktop().browse(new URI("https://www.google.com"));
                }
            }catch (Exception e) {
                // TODO: handle exception
            }
        }
            public static void main(String[]args) {
                new openBrowser();
            }
    }
    

    For your code you can follow the following steps

    • Download ChromeDriver from here
    • Extract the zip file and follow the path ( because it is easy ) C:\\chromeDriver\\chromedriver.exe
    • include the ChromeDriver location in your PATH environment variable
    • Download the required Libraries from the following junit openqa
    • Add the Libraries to your project ( Build Path )
    • then this is your code

      import java.util.logging.Level;
      import java.util.logging.Logger;
      import org.openqa.selenium.*;
      import org.openqa.selenium.chrome.*;
      import org.junit.Test;
      
      public class WebDriverDemo {
          @Test
          public static void main(String[] args) {
          System.setProperty("webdriver.chrome.driver", "C:\\chromeDriver\\chromedriver.exe");
          WebDriver driver = new ChromeDriver();
          driver.get("https://www.google.com/");
          WebElement searchBox = driver.findElement(By.name("q"));
          searchBox.sendKeys("ChromeDriver");
          searchBox.submit();
              try {
                  Thread.sleep(10000);
      
              } catch (InterruptedException ex) {
                  Logger.getLogger(WebDriverDemo.class.getName()).log(Level.SEVERE, null, ex);
              }
              driver.quit();
          }
      }
      

    During the implementation of the code in the eclipse, many problems occurred, so I advise you to implement the project on NetBeans I use Java 8 and Windows 8.1

    0 讨论(0)
提交回复
热议问题