【问题标题】:How to quit the page if i type hello world on google search using selenium?如果我使用 selenium 在谷歌搜索中输入 hello world,如何退出页面?
【发布时间】:2017-09-22 07:36:03
【问题描述】:

大家好,我想在使用 Firefox 浏览器和 selenium 在谷歌搜索中输入“Hello World”后退出页面

WebDriver driver = null;

public static void main(String args[]) {

    SimpleSelenium ss = new SimpleSelenium();
    ss.openBrowser();
    ss.getPage();
    ss.quitPage();
}

private void openBrowser() {

    System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
    driver = new FirefoxDriver();

}

private void quitPage() {
    driver.quit();

}

private void getPage() {
    driver.get("http://www.google.com");

}

【问题讨论】:

  • 你的测试类在哪里,之前和之后的方法?你已经写了一个独立的java程序
  • 你想达到什么目标,有什么问题?
  • 我想在谷歌搜索中输入 hello world 后退出页面

标签: java selenium mozilla


【解决方案1】:
1) Create a Junit test class
2) Initialize the driver in your setup method like 
ChromeDriver driver = new ChromeDriver();//Download chromeDriver.exe file and point to location where you have installed the like as you mentioned. `driver.System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");`
3) Create a test method with your business logic to type hello world
3) Create After and Before Class annotations for the methods .In After class annotation method you can write driver.quit.

您可以参考以下链接以获得更清晰的信息 https://www.guru99.com/selenium-tutorial.html

【讨论】:

  • 兄弟你可以使用任何驱动程序。这是你的愿望。但逻辑保持不变。我只是给出了一个示例代码:)
  • 你可以通过这个链接absofttrainings.com/…
【解决方案2】:

我添加了使用java和testNG编写的示例格式..这里每次首先在方法运行之前然后第一个测试用例将执行然后在方法之后将再次在方法工作之前然后下一个测试用例...... .这样你可以管理你的测试用例,它也会生成报告。Here你会得到更好的解释。

public class GoogleTest {
        FirefoxDriver driver;

        @BeforeMethod
        public void setUp1() throws Exception {
    System.setProperty("webdriver.gecko.driver", "D:\\\\ToolsQA\\trunk\\Library\\drivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.google.com");

            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        }


        @Test
        public void GoogleInputField() throws InterruptedException {
        System.out.println("Hello world");
        System.out.println("Hello world");
            //Write Your test case for test case 1


        }
        @Test
        public void google suggestion() throws InterruptedException {

            //Write Your test case for test case 1


        }

        @AfterMethod
        public void getResult(ITestResult result) throws IOException {

            driver.quit();
        }

    }

别忘了在 gecko.driver 路径上添加 Firefox 驱动程序

【讨论】:

    【解决方案3】:

    我假设您想使用 selenium 打开浏览器,加载 google 然后收听,直到您在输入框中手动输入“hello world”。方法listenForHelloWorld() 会做到这一点。

    public static void main(String args[]) {
    
        SimpleSelenium ss = new SimpleSelenium();
        ss.openBrowser();
        ss.getPage();
        ss.listenForHelloWorld();
        ss.quitPage();
    }
    
    private void listenForHelloWorld() {
        // Get the search field    
        WebElement searchField = driver.findElement(By.name("q"));
        int count = 1;
        while (count++ < 20) {
            // if search field value is "hellwo world" break loop which will eventallu lead to `quit()` as it is the next method to exit.
            if (searchField.getAttribute("value").equalsIgnoreCase("hello world")) {
                break;
            }
            Thread.sleep(5000)
        }
    }
    

    如果您询问如何在浏览器中自动输入“hello world”,请使用下方。

    driver.findElement(By.name("q")).sendKeys("hello world");
    

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 2018-12-19
      • 2017-07-08
      • 2023-03-20
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多