-3

我目前正在这里学习 Selenium 教程,并且我已经完全按照每个步骤进行操作,但是我的 Eclipse 程序不断抛出错误。我使用的是 Selenium 3,而本教程适用于旧版本。除了这个之外,我找不到任何全面的教程。如何修复以下代码中的错误?我已经评论了我在每一行之后得到的确切错误。代码已经包含一些注释,因此请忽略行首的任何注释。其他一切都应该是错误消息。

我还需要知道如何使用 Eclipse 设置我的类路径,以允许它访问 GeckoDriver,这可能会或可能不会解决问题。

public class Gmail_Login { //Syntax error on token(s), misplaced construct(s)
import org.openqa.selenium.By; //The import org.openqa.selenium.By cannot be resolved
import org.openqa.selenium.WebDriver; // The import org.openqa.selenium.WebDriver cannot be resolved
import org.openqa.selenium.WebElement;// The import org.openqa.selenium.WebElement cannot be resolved
import org.openqa.selenium.firefox.FirefoxDriver;// The import org.openqa.selenium.firefox. cannot be resolved

    /**

    * @param args

    */

           public static void main(String[] args) { //Multiple markers at this line -Syntax error,insert "enum Identifier" to complete EnumHeader   -Syntax error on tokens, AnnotationName expected instead   -Syntax error on token "}",invalid (    -Syntax error, insert")" to complete SingleMemberAnnotation    -Syntax error, insert "]" to complete ArrayAccess



    // objects and variables instantiation

                  WebDriver driver = new FirefoxDriver();//Multiple markers at this line   -FirefoxDriver cannot be resolved to a type   -WebDriver cannot be resolved to a type

                  String appUrl = "https://accounts.google.com";



    // launch the firefox browser and open the application url

                  driver.get(appUrl);



    // maximize the browser window

                  driver.manage().window().maximize();



    // declare and initialize the variable to store the expected title of the webpage.

                  String expectedTitle = " Sign in - Google Accounts ";



    // fetch the title of the web page and save it into a string variable

                  String actualTitle = driver.getTitle();



    // compare the expected title of the page with the actual title of the page and print the result

                  if (expectedTitle.equals(actualTitle))

                  {

                         System.out.println("Verification Successful - The correct title is displayed on the web page.");

                  }

                 else

                  {

                         System.out.println("Verification Failed - An incorrect title is displayed on the web page.");

                  }


    // enter a valid username in the email textbox

                  WebElement username = driver.findElement(By.id("Email"));//Multiple markers at this line   -WebElement cannot be resolved to a type

                  username.clear();

                  username.sendKeys("TestSelenium");


    // enter a valid password in the password textbox

                  WebElement password = driver.findElement(By.id("Passwd")); //Multiple markers at this line    -WebElement cannot be resolved to a type   -By cannot be resolved    -By cannot be resolved    


                  password.clear(); 
                  password.sendKeys("password123");



    // click on the Sign in button

                  WebElement SignInButton = driver.findElement(By.id("signIn")); //Multiple markers at this line   -WebElement cannot be resolved to a type     -By cannot be resolved 

                  SignInButton.click();


    // close the web browser

                  driver.close();

                  System.out.println("Test script executed successfully.");


    // terminate the program

                  System.exit(0);
           }






}//Syntax error on token "}", delete this token
4

2 回答 2

0

我认为我所做的只是将导入移出类并删除了一个},错误就消失了。我修复了缩进并删除了所有与错误相关的注释。尝试这个...

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Gmail_Login
{
    public static void main(String[] args)
    {
        // objects and variables instantiation
        WebDriver driver = new FirefoxDriver();
        String appUrl = "https://accounts.google.com";

        // launch the firefox browser and open the application url
        driver.get(appUrl);

        // maximize the browser window
        driver.manage().window().maximize();

        // declare and initialize the variable to store the expected title of the webpage.
        String expectedTitle = " Sign in - Google Accounts ";

        // fetch the title of the web page and save it into a string variable
        String actualTitle = driver.getTitle();

        // compare the expected title of the page with the actual title of the page and print the result
        if (expectedTitle.equals(actualTitle))
        {
            System.out.println("Verification Successful - The correct title is displayed on the web page.");
        }
        else
        {
            System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
        }

        // enter a valid username in the email textbox
        WebElement username = driver.findElement(By.id("Email"));
        username.clear();
        username.sendKeys("TestSelenium");

        // enter a valid password in the password textbox
        WebElement password = driver.findElement(By.id("Passwd"));
        password.clear();
        password.sendKeys("password123");

        // click on the Sign in button
        WebElement SignInButton = driver.findElement(By.id("signIn"));
        SignInButton.click();

        // close the web browser
        driver.close();
        System.out.println("Test script executed successfully.");
        // terminate the program
        System.exit(0);
    }
}
于 2016-10-18T17:20:47.720 回答
0

我认为我所做的一切都是将导入移出课堂并删除一个},并且错误消失了。我修正了缩进并消除了所有与错误相关的评论。尝试以下编码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Gmail_Login
{
    public static void main(String[] args)
    {
        // objects and variables instantiation
        WebDriver driver = new FirefoxDriver();
        String appUrl = "https://accounts.google.com";

        // launch the firefox browser and open the application url
        driver.get(appUrl);

        // maximize the browser window
        driver.manage().window().maximize();

        // declare and initialize the variable to store the expected title of the webpage.
        String expectedTitle = " Sign in - Google Accounts ";

        // fetch the title of the web page and save it into a string variable
        String actualTitle = driver.getTitle();

        // compare the expected title of the page with the actual title of the page and print the result
        if (expectedTitle.equals(actualTitle))
        {
            System.out.println("Verification Successful - The correct title is displayed on the web page.");
        }
        else
        {
            System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
        }

        // enter a valid username in the email textbox
        WebElement username = driver.findElement(By.id("Email"));
        username.clear();
        username.sendKeys("TestSelenium");

        // enter a valid password in the password textbox
        WebElement password = driver.findElement(By.id("Passwd"));
        password.clear();
        password.sendKeys("password123");

        // click on the Sign in button
        WebElement SignInButton = driver.findElement(By.id("signIn"));
        SignInButton.click();

        // close the web browser
        driver.close();
        System.out.println("Test script executed successfully.");
        // terminate the program
        System.exit(0);
    }
}
于 2020-12-24T15:58:38.940 回答