问题
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 at an executable to bring up chrome then designate a website to check. It looks like i am stuck in a loop?
Here is the program: java program
Here is the result: program result
any and all help would be appreciated. Thank you for your time.
回答1:
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
来源:https://stackoverflow.com/questions/59361521/opening-chrome-in-selenium-issue