gpt4 book ai didi

java - Selenium 中打开两个浏览器

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

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

谁能帮忙解决如何停止打开两个浏览器的问题。

最佳答案

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

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

关于java - Selenium 中打开两个浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409994/

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