gpt4 book ai didi

java - 我的java代码没有错误但停在nextInt

转载 作者:行者123 更新时间:2023-12-02 02:40:59 26 4
gpt4 key购买 nike

我正在编写一个 Java 代码,它将在 Chrome 或 Firefox 中运行一个简单的自动化场景 - 具体取决于用户的输入。没有下划线错误,但程序卡在 nextInt 行(在调试器中看到),并且没有输出。有人可以帮忙吗?谢谢!

package com.selenium;

import java.util.Scanner;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {

public static void main(String[] args) throws InterruptedException {
// environment variable
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\libs\\Drivers\\chromedriver.exe");
//WebDriver chromeDriver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver", "C:\\Automation\\libs\\Drivers\\geckodriver.exe");
WebDriver driver = null;

Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
System.out.println("Please enter 1 for Chrome or 2 for Firefox " + option);
if (option == 1)
{
WebDriver driver1= new FirefoxDriver();
}
else if
(option == 2)
{
WebDriver driver2 = new ChromeDriver();
}
else
System.out.println("Please enter a correct number " + option);

String baseURL = "https://login.salesforce.com/?locale=eu";

driver.get(baseURL);

WebElement userName = driver.findElement(By.id("username"));
userName.sendKeys("Yan");

WebElement password = driver.findElement(By.id("password"));
password.sendKeys("123456");

WebElement rememberCheckbox = driver.findElement(By.id("rememberUn"));
rememberCheckbox.click();

WebElement bLogin = driver.findElement(By.id("Login"));
bLogin.click();

}

}

最佳答案

System.out 放在 scanner.nextInt() 之前

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter 1 for Chrome or 2 for Firefox " + option);
int option = scanner.nextInt();

然后,当要求您输入数字时,在键盘上键入 1 或 2。

关于java - 我的java代码没有错误但停在nextInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com