gpt4 book ai didi

java - Selenium - Java//将浏览器实例传递给另一个方法

转载 作者:行者123 更新时间:2023-12-02 10:19:30 27 4
gpt4 key购买 nike

我目前正在尝试使用 Selenium 自动化 Web 流程,而且我对 Java 和 Selenium 还很陌生。我的主要方法是“当前打开一个网页”,对其进行一些工作,然后调用我的子方法之一(该方法也应该在同一个 excat 网页实例上工作)。相反,被调用的方法打开一个新的浏览器实例,显然最终会产生错误。到目前为止,我能够通过谷歌搜索此类主题的大部分问题,但我似乎无法找到将浏览器实例传递给被调用方法的解决方案。很高兴您能提供任何提示:-)

package test;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public classautomationtest {
public static void main(String[] args) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();

driver.navigate().to("some webside"); //didnt put the url in for this post

//.....

//doing loads of stuff on the webside (works perfectly fine)

//.....

//calling for the 2nd methode
methode_two(value1,value2,value3,value4);

}

//this method gets called correctly, but it opens its own browser instance, even though i want it to work on the one the main method did
public static void methode_two(int value1,int value2,int value3,int value4) {

String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();

//doing some stuff on the webside
//just a example line:
driver.findElement(By.cssSelector("div.field:nth-child(5) > input:nth-child(1)")).click(); //works like a charm in the main method


}

}

最佳答案

您需要将 webDriver 实例作为参数传递给函数:

package test;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public classautomationtest {
public static void main(String[] args) {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectLocation+"\\lib\\Geckodriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();

driver.navigate().to("some website"); //didn't put the url in for this post

//.....

//doing loads of stuff on the website (works perfectly fine)

//.....

//calling for the 2nd method
method_two(driver, value1, value2, value3, value4);

}

public static void method_two(WebDriver driver, int value1,i nt value2, int value3, int value4) {

String projectLocation = System.getProperty("user.dir");

//doing some stuff on the webside
driver.findElement(By.cssSelector("div.field:nth-child(5) > input:nth-child(1)")).click();


}

关于java - Selenium - Java//将浏览器实例传递给另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54444287/

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