【问题标题】:Print WebElement variable name through WebDriverEventListener通过 WebDriverEventListener 打印 WebElement 变量名
【发布时间】:2015-09-11 20:01:51
【问题描述】:

我正在使用 EventFiringWebDriver 和 WebDriverEventListener。在侦听器中,我在clickOn(WebElement arg0, WebDriver arg1) 方法之后实现方法。

WebElement myaccountLink =driver.findElement(By.cssSelector("div.footer>div.links>ul>li>a[title='My Account']"));
myaccountLink.click();

听者:

public void afterClickOn(WebElement arg0, WebDriver arg1) {
    System.out.println("Clicked : "+arg0.toString());
}

结果是:

点击:-> css 选择器:div.footer>div.links>ul>li>a[title='My Account']]

但我想:

点击:myaccountLink

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:

    您可以尝试像这样构建一个自定义类:

    public class Selector {
        public String name;
        public By by;
    
        public Selector(String name, By by){
            this.name= name;
            this.by = by;
        }
    
        public String getName(){
            return name;
        }
    
        public By getBy(){
            return by;
        }
    }
    

    然后,在您的代码中:

    Selector myAccountLink = ("Link to 'My account'", By.cssSelector("div.footer>div.links>ul>li>a[title='My Account']"));
    System.out.println("Clicked: " + myAccountLink.getName());
    
    // Output: "Link to 'My account'"
    

    【讨论】:

      【解决方案2】:

      我愿意对此进行更正,但据我所知,这是不可能的。 Java 中不传递变量名,只传递值。

      【讨论】:

      • 我昨天简单地调查了一下。实际上,通过反思似乎是可能的。所以这就是你需要研究的。
      • 请在此处指定可以使用哪种反射方法。
      • 我不知道它是怎么做的。你最好看看自己,因为我会做的就是谷歌它。
      猜你喜欢
      • 2019-07-22
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2019-08-13
      • 2011-03-30
      相关资源
      最近更新 更多