1

Selenium を使用してページに移動し、Internet Explorer を使用してスクリーンショットを撮っています。しかし問題は、ログインが Javascript アラート ボックスによって処理されることです。現在、Selenium には、Alert 要素を使用してアラート ボックスにフォーカスを移動できる機能があり、フォーカスを移動して、ユーザー名のテキスト ボックスにいくつかの値を入力することもできました。

問題は、Selenium がフォーカスをパスワード テキスト ボックスに切り替えず、同じボックスにユーザー名とパスワードを入力することです。Java AWT Robot でタブ キーをクリックしてフォーカスを変更しようとしましたが、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();

ここで何が欠けていますか?ここでの私のアプローチは正しいですか、それとも別のルートをたどる必要がありますか?

4

5 に答える 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();
于 2013-03-21T10:00:53.707 に答える
0

Javaの回答ではありませんが、この問題に対する.netの回答を探しているこの質問を見つけたので。

.NET を使用している場合は、Robot ではなく SendKeys を使用する必要があります

using System.Windows.Forms;

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

これが誰かを助けることを願っています!

于 2013-09-17T17:25:26.430 に答える
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();
于 2016-04-29T10:18:54.277 に答える