【问题标题】:Selenium sendKeys does not work with an array using loopSelenium sendKeys 不适用于使用循环的数组
【发布时间】:2018-03-21 20:30:37
【问题描述】:

我制作了一个特殊字符数组,并希望使用 sendKeys 函数将这些特殊字符传递给每个输入字段。 下面的代码不起作用,我不知道为什么.. 如果有人能帮我解决这个问题,我真的很感激。

    String[] specialCharList = {"'",":","|","&"};

    for (int i = 0; i < specialCharList.length; i++) {


        WebElement spList1 = driver.findElement(By.id("primaryContact_firstNameCell"));
        spList1.sendKeys(specialCharList[i]);
        Thread.sleep(3000);

        WebElement spList2 = driver.findElement(By.id("primaryContact_,middleInitialCell"));
        spList2.sendKeys(specialCharList[i]);
        Thread.sleep(3000);  

        WebElement spList3 = driver.findElement(By.id("primaryContact_lastNameCell"));
        spList3.sendKeys(specialCharList[i]);
        Thread.sleep(3000);

【问题讨论】:

  • 你想达到什么目的?传递整个字符串是不可能的吗?您是否希望这些特定角色有一些特别的东西?请提供您认为不起作用的实际行为。
  • 我正在尝试将每个特殊字符发送到每个输入字段并单击保存以查看它是否返回任何错误。例如,我想向每个输入字段(例如名字和姓氏字段)发送一个单引号 ('),然后单击保存按钮。在这种情况下,它会迭代数组中的下一个特殊字符,即分号 (:)。我希望这对你有意义。
  • does not work 到底是什么意思?您是否收到任何错误/异常?
  • 我收到一条错误消息“未知错误:无法聚焦元素”..
  • 对不起,伙计们,我发现我输入了错误的 id... 使用正确的 id,这段代码可以完美运行。真的很抱歉,非常感谢您的帮助!

标签: java selenium automated-tests


【解决方案1】:

我已经在 Facebook 登录页面中尝试过您的代码,它似乎运行良好。

找到我尝试过的代码并参考屏幕截图。 请让我们知道您在尝试时遇到了什么样的错误。

String[] specialCharList = {"'",":","|","&"};
    for (int i = 0; i < specialCharList.length; i++) {
        WebElement spList1 = driver.findElement(By.xpath(".//*[@name='firstname']"));
        spList1.sendKeys(specialCharList[i]);
        Thread.sleep(3000);

        WebElement spList2 = driver.findElement(By.xpath(".//*[@name='lastname']"));
        spList2.sendKeys(specialCharList[i]);
        Thread.sleep(3000); 
       }

【讨论】:

  • 感谢您分享您的屏幕截图。我收到一条错误消息,提示“未知错误:无法聚焦元素”。你知道那是什么意思吗?
  • 仅供参考,我正在尝试在网站上测试申请表。
  • 嗨,Guna,很抱歉,原来我使用了错误的 ID……现在它可以完美运行了。很抱歉,但我非常感谢您的帮助。
  • 关于您打开屏幕截图时的错误 - 这是因为,我不允许在 stackoverflow 中附加屏幕截图,直到我在本网站(LoL)中获得不错的分数。我很高兴您找到了解决方案。
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多