将以前使用的字符串导入新方法

时间:2016-10-26 00:00:02

标签: java string selenium jpanel desktop

我想在新的JFrame窗口方法中使用一个字符串(在我的main方法中初始化)。我有以下代码:

public static void main(String[] args){
WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
String linkLocation = link.getAttribute("href");
}

我的主要方法和我的JPanel中的JButton的以下代码

public void actionPerformed(ActionEvent e) 
{
desk.browse(new URI(linkLocation));
}

如何让它发挥作用?

3 个答案:

答案 0 :(得分:0)

这样的事情可能对你有帮助:

public String LinkLocation(){
    WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
    String linkLocation = link.getAttribute("href");
    return linkLocation;
    }

public void actionPerformed(ActionEvent e) 
{
desk.browse(new URI(LinkLocation()));
}

答案 1 :(得分:0)

String之外定义main

public String linkLocation = " ";
public static void main(String[] args) {
    WebElement link = driver.findElement(By.xpath("//*[@id='pagecontainer']/section/ul/li[1]/a"));
    linkLocation = link.getAttribute("href");
}

您现在可以从其他地方引用linkLocation。如果您的新方法属于同一个类,只需输入linkLocation即可,否则请使用classname.linkLocation

答案 2 :(得分:0)

假设您在JButton提取的同一个地方有linkLocation的访问权限,请尝试JButton.setActionCommand()

public static void main(String[] args) {
   // ...
   String linkLocation = link.getAttribute("href");
   jButton.setActionCommand(linkLocation);
   // ..
}

现在你可以使用它,根据你的帖子,下面是按钮处理程序:

public void actionPerformed(ActionEvent e) {
  desk.browse(new URI(e.getActionCommand()));
}