运行Selenium测试时出现IllegalStateException

时间:2019-10-26 11:53:11

标签: selenium-webdriver

运行以下代码时,我会抛出IllegalStateException

package newprojectss;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Tours {

    public static void main(String[] args) {
        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.chrome.driver","C:\\selenium-2.25.0\\chromedriver_win32");
        WebDriver driver = new ChromeDriver();

        String baseUrl = "http://demo.guru99.com/test/newtours/";
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = "";

        // launch Fire fox and direct it to the Base URL
        driver.get(baseUrl);

        // get the actual value of the title
        actualTitle = driver.getTitle();

        /*
         * compare the actual title of the page with the expected one and print
         * the result as "Passed" or "Failed"
         */
        if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");
        }

        //close Firefox
        driver.close();

     }
}

什么会导致问题?

1 个答案:

答案 0 :(得分:0)

由于chrome驱动程序设置不正确,会出现非法状态异常

   System.setProperty("webdriver.chrome.driver","C:\\selenium-2.25.0\\chromedriver_win32") 

Chrome驱动程序路径直到chromedriver.exe文件都指向

解压缩您的驱动程序文件夹,然后将其指向chromedriver.exe

例如:C:// Users //用户名//Desktop//chromedriver.exe