1
我正在嘗試登錄到我的個人資料,但在嘗試添加密碼時遇到了問題。我可以讓光標顯示在密碼框中,但當試圖發送密鑰時,它會引發異常。這是否有一些安全原因?密碼是否隱藏在html/xml中,以便不會發生自動化?Selenium發送文本到密碼字段
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
真棒,工作。你知道這是爲什麼嗎?這是出於安全目的嗎? –
@ scarecrow-是的,看起來像一個簡單的對付網絡抓取機器人的措施。 – alecxe