在用户名和密码文本框之间切换,并使用Selenium WebDriver java输入相应的值

问题描述 投票:1回答:5

我正在使用selenium导航到页面并使用Internet Explorer截取屏幕截图。但问题是登录是由Javascript警报框处理。现在Selenium有一个工具,可以通过使用Alert元素将焦点带到警报框,我已设法引入焦点并在用户名文本框中输入一些值。

问题是Selenium没有将焦点切换到密码文本框并在同一个框中输入用户名和密码。我尝试使用Java AWT Robot点击tab键,它会改变焦点,但是Selenium没有识别出这一点,它继续在同一个框中输入用户名和密码。

以下是我的代码:

Robot robot = new Robot();
driver.get("the url");
Alert alert = driver.switchTo().alert();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);
alert.sendKeys("password");
alert.accept();

我在这里错过了什么?我的方法在这里是正确的还是我必须采取不同的路线?

java internet-explorer selenium-webdriver alert
5个回答
2
投票

嗨Madusudanan通过注释另一个切换方法来尝试代码。

Robot robot = new Robot();
Alert alert=dr.switchTo().alert();
dr.get("the url");
alert.sendKeys("username");
//dr.switchTo().alert();
robot.keyPress(KeyEvent.VK_TAB);
alert.sendKeys("password");
alert.accept();

0
投票

不是Java的答案,但是因为我发现这个问题正在寻找这个问题的.net答案。

如果您使用的是.NET,则需要使用SendKeys而不是Robot

using System.Windows.Forms;

        _driver.SwitchTo().Alert().SendKeys("Username");
        SendKeys.SendWait("{TAB}");
        SendKeys.SendWait("password");
        SendKeys.SendWait("{Enter}");

希望这有助于某人!


0
投票

根据你的问题,在关注密码字段后,Selenium WebDriver无法输入/输入/输入它的相应值。您可以使用Robot类键入密码值。以下是整个代码:

//First write a method to use StringSelection
public void setClipboardData(String string) {
            StringSelection stringSelection = new StringSelection(string);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

Robot robot = new Robot();
driver.get("Your URL");
Alert alert = driver.switchTo().alert();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//call setClipboardData method declared above
setClipboardData("Your Password");
//Copy Paste by using Robot class
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
alert.accept();

0
投票

我从任何源创建代码,并在添加之后工作:a.keyRelease(KeyEvent.VK_ADD);

          // Initialize InternetExplorerDriver Instance.
          WebDriver driver = new InternetExplorerDriver();

          // Load sample calc test URL.
          driver.get("your homepage testing");

          //Code to handle Basic Browser Authentication in Selenium.
          Alert aa = driver.switchTo().alert();

          Robot a = new Robot();
          aa.sendKeys("beyond"+"\\"+"DND");  

          a.keyPress(KeyEvent.VK_TAB);
          a.keyRelease(KeyEvent.VK_TAB);
          a.keyRelease(KeyEvent.VK_ADD);                

          setClipboardData("P@ssw0rd");
          a.keyPress(KeyEvent.VK_CONTROL);
          a.keyPress(KeyEvent.VK_V);
          a.keyRelease(KeyEvent.VK_V);
          a.keyRelease(KeyEvent.VK_CONTROL);
          aa.accept();    private static void setClipboardData(String string)StringSelection stringSelection = new StringSelection(string);

Toolkit.getDefaultToolkit()。getSystemClipboard()。setContents(stringSelection,null);


0
投票

使用java.awt.Robot类有两个主要缺点。

  1. Mac问题:在Mac OS上,当我们实例化Robot类时,Java应用程序在后台启动并消除焦点(因此社区中的一些人发送VK_META(Cmd键)和VK_TAB以返回警报)。由于Mac的复制和粘贴快捷键不同,因此还需要处理(使用VK_META代替VK_CONTROL)。
  2. Jenkins或远程转轮问题:即使我们解决了上述问题,最终在从Jenkins作业运行测试时,机器人实例化再次成为问题(Robotframework selenium execution through jenkins not working

幸运的是,sendKeys可以处理tab键(作为扫描码),下面的代码对我有用。

Alert alert = driver.switchTo().alert();
alert.sendKeys("username" + Keys.TAB.toString() + "password");
alert.accept();
© www.soinside.com 2019 - 2024. All rights reserved.