如何将驱动程序信息从一种方法传递给另一种方法

时间:2021-06-18 11:58:31

I am new to selenium.
Please help with my query. I have one method urlload in which I am loading a particular URL.
Suppose I am creating another method in same class or another class, I am unable to read webElements of webpage loaded in urlload method.
Please help.

我是硒的新手。请帮助我查询。我有一个方法urlload,我在其中加载一个特定的URL。假设我在同一个类或另一个类中创建另一个方法,我无法读取urlload方法中加载的网页的webElements。请帮忙。

public class loading {
    public static void urlload() {
        WebDriver driver = new ChromeDriver(Options);
        String baseurl = "http://www.google.com/";
        System.out.println(baseurl);
        driver.get(baseurl);
        driver.manage().window().maximize();
    }
}

2 个解决方案

#1


0  

using constructor you can pass driver reference.

使用构造函数可以传递驱动程序引用。

//constructor

public Classname(WebDriver driver) { this.driver = driver; }

public Classname(WebDriver驱动程序){this.driver = driver; }

#2


0  

Since you are new, try this. Its not about selenium but the programming language.

既然你是新手,试试吧。它不是关于硒而是编程语言。

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

public class Hello {
    public static void main(String[] args) throws Exception {
        WebDriver driver = setUp();
        driver = test(driver);
        tearDown(driver);
    }
public static WebDriver setUp() throws Exception {
    WebDriver driver = new ChromeDriver();
    String baseurl = "http://www.google.com/";
    System.out.println(baseurl);
    driver.get(baseurl);
    driver.manage().window().maximize();
    return driver;
}

public static WebDriver test(WebDriver driver) throws Exception {
    driver.findElement(By.linkText("Gmail")).click();
    return driver;
}

public static void tearDown(WebDriver driver) throws Exception {
    driver.quit();
}
}

#1


0  

using constructor you can pass driver reference.

使用构造函数可以传递驱动程序引用。

//constructor

public Classname(WebDriver driver) { this.driver = driver; }

public Classname(WebDriver驱动程序){this.driver = driver; }

#2


0  

Since you are new, try this. Its not about selenium but the programming language.

既然你是新手,试试吧。它不是关于硒而是编程语言。

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

public class Hello {
    public static void main(String[] args) throws Exception {
        WebDriver driver = setUp();
        driver = test(driver);
        tearDown(driver);
    }
public static WebDriver setUp() throws Exception {
    WebDriver driver = new ChromeDriver();
    String baseurl = "http://www.google.com/";
    System.out.println(baseurl);
    driver.get(baseurl);
    driver.manage().window().maximize();
    return driver;
}

public static WebDriver test(WebDriver driver) throws Exception {
    driver.findElement(By.linkText("Gmail")).click();
    return driver;
}

public static void tearDown(WebDriver driver) throws Exception {
    driver.quit();
}
}