两个浏览器在Selenium中打开

问题描述 投票:0回答:3

我使用两个类,一个是BaseCode,其中声明了所有基本方法。该类定义如下:

package CodeBase;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BaseCode {

    //All the Universal variables for TestBase class are declared here:
        public static WebDriver driver = null;

    //All Universal Methods for TestBase class are declared here:
            public void launchBrowser(String baseUrl) throws Exception{
                try {
                        System.out.println("Launching the Chrome Browser");
                        String driverpath = "E:\\Learning\\Selenium\\Drivers\\ChromeDriver\\chromedriver.exe";
                        System.setProperty("webdriver.chrome.driver",driverpath);
                        driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        System.out.println("Opening URL: " + baseUrl);
                        driver.navigate().to(baseUrl);
                        TimeUnit.SECONDS.sleep(5);
            }catch(Exception E) {
                System.out.println(E.getMessage() +"\n" + E.getStackTrace());
                }
            }
}

现在我使用上面的launchBrowser()的第二课如下:

package projectpack;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import CodeBase.BaseCode;



public class Flipkart {

    public static BaseCode B = new BaseCode();

  @Test
  public void testFlipkart() {
      String url = "https://www.flipkart.com/";
      try {
            B.launchBrowser(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        WebDriver driver = new ChromeDriver();
        try {
        TimeUnit.SECONDS.sleep(5);
        this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Intro.jpeg");
        String Prod1 = "philips bt120";
        WebElement Search = driver.findElement(By.cssSelector("._1WMLwI .LM6RPg"));
        Search.sendKeys(Prod1);
        this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Step1.jpeg");
        Search.submit();
        this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Step2.jpeg");
        WebElement Object = driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[1]/div/div[2]/div/div[2]/div/div[3]/div/div/div[1]/div/a[2]"));
        Object.click();
        this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Step3.jpeg");
        driver.quit();
        }catch(Exception E) {
            System.out.println(E.getMessage() + "\n" + E.getStackTrace());
        }
  }
}

当我运行此代码时,它打开两个浏览器而不是一个。第一个已经重定向到flipkart.com,但第二个只显示一个空白窗口。控制台输出如下:

[RemoteTestNG] detected TestNG version 6.12.0
Launching the Chrome Browser
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 47688
Only local connections are allowed.
Sep 25, 2017 9:58:20 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Opening URL: https://www.flipkart.com/
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 47270
Only local connections are allowed.
Sep 25, 2017 9:58:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
no such element: Unable to locate element: {"method":"css selector","selector":"._1WMLwI .LM6RPg"}
  (Session info: chrome=60.0.3112.113)
  (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

如果它停留在同一浏览器中,它将找到Search WebElement但它打开第二个浏览器。

任何人都可以帮助如何停止打开两个浏览器。

java selenium selenium-webdriver
3个回答
0
投票

您正在调用以下代码两次:

driver = new ChromeDriver();

一旦进入LaunchBrowser方法,就在try catch之后。这就是你得到两个浏览器的原因。


0
投票

您在类中的第一个函数中初始化chrome驱动程序2次

driver = new ChromeDriver();

0
投票

我有同样的问题,但我发现在所有其他testNG注释之前我有“@BeforeSuite(alwaysRun = true)”。我在代码中没有@afterSuite(它可能忘记在其中一个测试后删除它。

脱下后它得到修复。

© www.soinside.com 2019 - 2024. All rights reserved.