【问题标题】:Taking in Webdriver element name as a parameter以 Webdriver 元素名称为参数
【发布时间】:2017-11-06 10:03:39
【问题描述】:

所以目前我有这个代码:

      //WebElement VariableName is to declare the name of the WebElement 
      //String Id is the Id of the element that you want to send values to
      //String valuesToAddIn is the value that you want to enter into the webpage
      //String stringNameToCompareLater is the variable to store the values of the input, that you just entered.

      public void AddInValueAndCompareString(WebElement VariableName ,String Id , String valuesToAddIn, String stringNameToCompareLater)
      {
        VariableName = driver.findElement(By.id(Id));
        VariableName.sendKeys(valuesToAddIn);
        EnsureUserInputByID(Id);
        stringNameToCompareLater = VariableName.getText();
      }

我想做的是获取我刚刚使用 selenium 输入的字符串并将其存储在一个变量中。

但是,如果您要为代码的每一部分添加这些行,将会非常麻烦。

所以我上面代码的实现如下:

AddInValueAndCompareString(CourseFeePaid,"courseFeePaid","123.90","courseFeePaidCompare");

但是,CourseFeePaid 实际上给了我

的错误
CourseFeePaid cannot be resolved to a variable

是我无法在参数中声明WebElement的名称,还是代码中存在任何其他错误。

我想要得到的最终成果是:

  1. 值被添加到文本框中
  2. 值存储在变量中,以便以后进行比较。

【问题讨论】:

    标签: java eclipse selenium


    【解决方案1】:

    你可以在这里查看同样的问题Java, "Variable name" cannot be resolved to a variable 我猜您的问题不是在“AddInValueAndCompareString”方法中,而是在您调用此方法时。 您的实现有太多参数: 1.传递WebElement变量名和在第一行调用VariableName = driver.findElement(By.id(Id));的意义你可以删除第一个参数,第一行将是WebElement VariableName = driver.findElement(By.id(Id)); 2. 您作为参数 stringNameToCompareLater 传递,但在第 4 行中您再次有一个赋值 stringNameToCompareLater = VariableName.getText(); 为什么不从参数中删除 stringNameToCompareLater 并在第 4 行中有 String stringNameToCompareLater = VariableName.getText();。 所以你传递了两个带值的变量,但从不使用它们,它们实际上是局部变量。这没有意义。

    是的,您可以输入文本。但是您需要在键入后查看它的存储位置。例如。它可能在 'value' 属性中,所以你可以像这样得到它:String enteredtext = VariableName.getAttribute("value"); 但它取决于页面,请记住,VariableName.getText(); 并不总是有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 2019-11-07
      • 2017-03-04
      相关资源
      最近更新 更多