Selenium HtmlUnitDriver 登录网站
Selenium HtmlUnitDriver Login to Website
我正在尝试使用 HtmlUnitDriver 登录 marketwatch.com。我认为该程序永远不会通过登录提示,而且我确实检查了我的电子邮件和密码是否正确...
非常感谢你试图帮助我!
这是我的注释代码:
static Logger log = Logger.getLogger("com.gargoylesoftware");
public static void main(String[] args) throws InterruptedException {
log.setLevel(Level.OFF);
// Create a new instance of the html unit driver
WebDriver driver = new HtmlUnitDriver();
// Go to marketwatch
driver.get("http://www.marketwatch.com/game/");
System.out.println(driver.getTitle()); // prints 'Virtual Stock Exchange Games (VSE) - MarketWatch.com'
// Click on 'login'
WebElement login = driver.findElement(By.id("profilelogin"));
login.click();
System.out.println(driver.getTitle()); // prints 'Log In'
// Enter username and password
WebElement email = driver.findElement(By.id("username"));
email.sendKeys(Ref.EMAIL);
WebElement pass = driver.findElement(By.id("password"));
pass.sendKeys(Ref.PASSWORD);
// Click submit button
WebElement submit = driver.findElement(By.id("submitButton"));
submit.click();
System.out.println(driver.getTitle()); // prints 'Log In' (same title as before we tried to log in)
// Try to find
WebElement game = driver.findElement(By.xpath("//*[@id='maincontent']/section[1]/table/tbody/tr[1]/td[1]/a"));
// This gets printed to console:
// Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[@id='maincontent']/section[1]/table/tbody/tr[1]/td[1]/a
// I don't think it's ever getting past the login screen...
// You don't really have to read past this, unless you think I might've messed something else up and you want to fix it.
game.click();
WebElement trade = driver.findElement(By.xpath("//*[@id='vse-nav']/ul/li[2]/a"));
trade.click();
WebElement searchBar = driver.findElement(By.className("instrumentsearch ac_input unused"));
searchBar.sendKeys("GOOG");
searchBar.submit();
System.out.println(driver.getTitle());
driver.quit();
}
在您的 submit.click() 之后,您可以转储 selenium-attached 浏览器的当前 DOM,您可以检查它以确定 Selenium 实际遇到了什么。通过以下代码执行此操作可能允许您查看任何适用的错误消息...例如,可能有一个错误表明不允许机器人访问该站点。
System.out.println(driver.getPageSource());
尝试启用javascript,网站需要它:
// Create a new instance of the html unit driver
WebDriver driver = new HtmlUnitDriver(true);
您也可以通过以下方式启用 JavaScript:
driver.setJavascriptEnabled(true);
我正在尝试使用 HtmlUnitDriver 登录 marketwatch.com。我认为该程序永远不会通过登录提示,而且我确实检查了我的电子邮件和密码是否正确...
非常感谢你试图帮助我!
这是我的注释代码:
static Logger log = Logger.getLogger("com.gargoylesoftware");
public static void main(String[] args) throws InterruptedException {
log.setLevel(Level.OFF);
// Create a new instance of the html unit driver
WebDriver driver = new HtmlUnitDriver();
// Go to marketwatch
driver.get("http://www.marketwatch.com/game/");
System.out.println(driver.getTitle()); // prints 'Virtual Stock Exchange Games (VSE) - MarketWatch.com'
// Click on 'login'
WebElement login = driver.findElement(By.id("profilelogin"));
login.click();
System.out.println(driver.getTitle()); // prints 'Log In'
// Enter username and password
WebElement email = driver.findElement(By.id("username"));
email.sendKeys(Ref.EMAIL);
WebElement pass = driver.findElement(By.id("password"));
pass.sendKeys(Ref.PASSWORD);
// Click submit button
WebElement submit = driver.findElement(By.id("submitButton"));
submit.click();
System.out.println(driver.getTitle()); // prints 'Log In' (same title as before we tried to log in)
// Try to find
WebElement game = driver.findElement(By.xpath("//*[@id='maincontent']/section[1]/table/tbody/tr[1]/td[1]/a"));
// This gets printed to console:
// Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[@id='maincontent']/section[1]/table/tbody/tr[1]/td[1]/a
// I don't think it's ever getting past the login screen...
// You don't really have to read past this, unless you think I might've messed something else up and you want to fix it.
game.click();
WebElement trade = driver.findElement(By.xpath("//*[@id='vse-nav']/ul/li[2]/a"));
trade.click();
WebElement searchBar = driver.findElement(By.className("instrumentsearch ac_input unused"));
searchBar.sendKeys("GOOG");
searchBar.submit();
System.out.println(driver.getTitle());
driver.quit();
}
在您的 submit.click() 之后,您可以转储 selenium-attached 浏览器的当前 DOM,您可以检查它以确定 Selenium 实际遇到了什么。通过以下代码执行此操作可能允许您查看任何适用的错误消息...例如,可能有一个错误表明不允许机器人访问该站点。
System.out.println(driver.getPageSource());
尝试启用javascript,网站需要它:
// Create a new instance of the html unit driver
WebDriver driver = new HtmlUnitDriver(true);
您也可以通过以下方式启用 JavaScript:
driver.setJavascriptEnabled(true);