两个浏览器在Selenium中打开

时间:2017-09-25 16:31:39

标签: java selenium selenium-webdriver

我使用两个类,一个是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,但它会打开第二个浏览器。

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

4 个答案:

答案 0 :(得分:1)

我在Selenium C#Visual Studio中遇到了类似的问题。事实证明,我的项目中还有另一个类已初始化WebDriver driver = new ChromeDriver();。并将其放在我的主要test_class上。

我如何解决它: 我从Main_class中删除了它,并在那里创建了它的实例,然后使用了它。

答案 1 :(得分:0)

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

driver = new ChromeDriver();

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

答案 2 :(得分:0)

您在类

中的函数1中初始化chrome驱动程序2次
driver = new ChromeDriver();

答案 3 :(得分:0)

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

取下后,它得到修复。