java - 错误: Driver cannot be resolved to a variable- selenium java eclipse

标签 java eclipse selenium webdriver selenium-chromedriver

我正在尝试习惯使用 Selenium 进行自动化测试。到目前为止,我已经成功完成了该测试用例的所有方面,除了最后一步,即检查警报是否存在(收到交易确认)

我尝试使用 boolean 值来执行此函数,但我不断收到相同的错误:驱动程序无法解析为变量。

有什么建议吗?

package com.scotia.test;      

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ScotiaTest1 {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","/Users/theone/Downloads/chromedriver-new");           

        // Create a new instance of the Chrome driver
        WebDriver driver = new ChromeDriver();                      

        //Login using Username and Password
        driver.get("https://LEAP:Password10@apps.scotiabank.com/LEAP_Prototype/desktop/html/Chile_index.html#"); 

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Find Proximity Serie A link and click on it
        driver.findElement(By.cssSelector("#investing_tab tr:nth-child(7) a.acct-name")).click();

        //Wait for 5 Sec
        Thread.sleep(2000); 

        //Find New Funds Button and click on it
        driver.findElement(By.cssSelector(".pad-top-10.txt-right .btn.action-btn")).click();

         //Wait for 5 Sec
        Thread.sleep(2000);

        //Select Field Rescue investment and choose Rescue
        Select dropdown = new Select(driver.findElement(By.id("mf_action")));
        dropdown.selectByVisibleText("Inversión");

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Select Field Current account and choose current account
        dropdown = new Select(driver.findElement(By.id("selaccount_drpdown")));
        dropdown.selectByVisibleText("Cuenta Corriente *** 0002 USD 10.000,00");

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Select Field Fund Type and choose medium term
        dropdown = new Select(driver.findElement(By.id("term")));
        dropdown.selectByVisibleText("Mediano Plazo");

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Select Field Mutual Fund Name and choose Proximity Series A
        dropdown = new Select(driver.findElement(By.id("typefund")));
        dropdown.selectByVisibleText("Proximidad Serie A");

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Select Field Fund account Name and choose 001
        dropdown = new Select(driver.findElement(By.id("sub_accnt")));
        dropdown.selectByVisibleText("001");

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Select Field Rode and type 222
        driver.findElement(By.id("amount_field")).sendKeys("222");

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Find to Accept Button and click on it
        driver.findElement(By.id("next")).click();

        //Wait for 5 Sec
        Thread.sleep(2000);

        //Find to Confirm Button and click on it
        driver.findElement(By.id("next")).click();

        //Wait for 5 Sec
        Thread.sleep(2000);

        // Print a Log In message to the screen

         //Wait for 5 Sec
        Thread.sleep(2000);
    }       

    public boolean isAlertPresent(){ 
        try{ 
            Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
            if(a!=null){
                System.out.println("Alert is present");
                driver.switchTo().alert().accept();
                return true;
            }else{
                throw new Throwable();
            }
        } 
        catch (Throwable e) {
            System.err.println("Alert isn't present!!");
            return false; 
        }   

        //Wait for 5 Sec
        Thread.sleep(2000);        
   }

}

最佳答案

这是您正在使用的实际代码吗?它有很多问题: isAlertPresent() 没有被调用,你在那里使用驱动程序,但它不是类的字段,等等(因此你得到“错误:驱动程序无法解析为变量”)
我将其修改为可运行(将 isAlertPresent() 合并到 main() 中),并在最后得到了绿色的“警报”。你是这个意思吗?如果是,它不是一个警报,它只是一个 div,所以不要这样:

Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());

写这样的东西:

WebElement div = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.className("success-msg")));

如果情况并非如此,请使用您想要的警报编辑您的问题。 enter image description here

关于java - 错误: Driver cannot be resolved to a variable- selenium java eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40875580/

相关文章:

java - 从 youtube 客户端 api 获取视频源

java - 从最后一行读取文件

java - 如何在 Eclipse IDE 中删除圆形选项卡?

python - 在 mac 中使用 python 为 safari 浏览器设置 selenium

java - Selenium - 通过 url 进行基本身份验证

java - 有没有什么方法可以使用 Spring Social、restfb 等从公共(public) Facebook 页面获取提要

java - Apache Camel 文件组件 : Specifying 1 file in regex

java - 在 Eclipse 中调试 Java 代码时看不到变量

eclipse - 如何在 Eclipse 中使 Scala 隐式显式化

ruby - 使用 Selenium 和 Ruby 将键盘快捷键发送到 chrome