2016-10-18 47 views
-3

我目前正在參加Selenium教程Here,我完全按照每一步操作,但是我的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 
+1

不要進口到課外?就像你在學習的教程一樣?另外,由於您的目標是Selenium的不同版本,因此您可能無法隨機假設所有內容都是相同的。 –

+0

硒3是非常新的。 (5天前發佈)。可以肯定的是,全面的教程還沒有被寫入Selenium 3 :) – sircapsalot

+0

@DaveNewton即使當我把它們放在課堂外時,所有相同的錯誤也會返回。我的印象是,我想在課堂外定義/導入它們,以便稍後將它們引用到範圍中。我不確定Selenium是否有這種能力。 –

回答

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); 
    } 
} 
+0

我複製/粘貼到我的客戶端,並分別重命名包和類。它會拋出.sendKeys()的錯誤,表示該參數不適用於字符串。 –

+0

看着[這個頁面](http://stackoverflow.com/questions/23485363/the-method-sendkeyscharsequence-in-the-type-webelement-is-not-applicable-for)它聽起來像你可能正在使用Java的真正舊版本?我會確保您安裝了最新的JRE和Selenium版本以及您可能使用的任何其他庫。 – JeffC

+0

我的庫是最新的,事實證明我只需要將我的編譯器遵從性設置爲1.7。 –