【问题标题】:Selenium sending text to password fieldSelenium 将文本发送到密码字段
【发布时间】:2016-12-07 18:18:16
【问题描述】:

我正在尝试登录我的个人资料,但在尝试添加密码时遇到了问题。我可以让光标显示在密码框中,但是在尝试发送密钥时,它会抛出异常。这有什么安全原因吗?密码是否隐藏在 html/xml 中以防止自动化发生?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class mainTester {

 static WebDriver driver;

public static void main(String[] args) throws InterruptedException{

    setUp();
}
public static void setUp() throws InterruptedException {
      // Optional, if not specified, WebDriver will search your path for chromedriver.
      System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");

      driver = new ChromeDriver();
      driver.get("http://www.google.com/xhtml");
    //  Thread.sleep(5000);  // Let the user actually see something!
      WebElement searchBox = driver.findElement(By.name("q"));
      searchBox.sendKeys("C programming forum");
      searchBox.submit();
      Thread.sleep(5000);
      driver.findElement(By.linkText("C Board - Cprogramming.com")).click();
      Thread.sleep(5000);  // Let the user actually see something!
      WebElement userEnter = driver.findElement(By.name("vb_login_username"));
      userEnter.sendKeys("myusername");
      WebElement userEnter2 = driver.findElement(By.name("vb_login_password_hint"));
      userEnter2.sendKeys("mypassword");
     // searchBox.submit();
      Thread.sleep(5000);  // Let the user actually see something!
      System.out.println("Finished");
      //driver.quit();

    }

}

html是这样的

<input type="text" class="textbox default-value" tabindex="102" name="vb_login_password_hint" id="navbar_password_hint" size="10" value="Password" style="display: inline;">

错误是

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element

【问题讨论】:

    标签: java google-chrome selenium


    【解决方案1】:

    这个登录表单有点棘手,有两个密码输入 - 一个就像在实际密码字段上显示的“提示”。您需要做的是先点击“提示”,然后将密钥发送到真正的密码输入:

    WebElement passwordHint = driver.findElement(By.name("vb_login_password_hint"));
    passwordHint.click();
    
    WebElement passwordInput = driver.findElement(By.name("vb_login_password"));
    passwordInput.sendKeys("mypassword");
    

    【讨论】:

    • 太棒了,成功了。你知道为什么会这样吗?这是出于安全目的吗?
    • @scarecrow- 是的,看起来像是对网络抓取机器人的简单对策。
    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 2015-01-27
    • 2019-11-30
    • 2023-03-07
    • 1970-01-01
    • 2016-02-11
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多