Java Selenium:成功登录后无法获取任何元素

时间:2014-08-11 08:38:52

标签: java selenium selenium-webdriver

我使用selenium登录网站https://launch.stellar.org/#/login。但我无法在新页面中获得任何元素。

这里是代码:

public class SeleniumProcessor {
public WebDriver driver;


public SeleniumProcessor() {
    this.driver = new FirefoxDriver();
}

public void openUrl(String url) {
    driver.get(url);
}

public void login(String userName, String password) {
    WebElement userNameTxt = driver.findElement(By.id("username"));
    WebElement passwordTxt = driver.findElement(By.id("password"));
    userNameTxt.sendKeys(userName);
    passwordTxt.sendKeys(password);
    passwordTxt.sendKeys(Keys.RETURN);
}

public static void main(String[] args) throws InterruptedException {
    SeleniumProcessor login = new SeleniumProcessor();
    login.openUrl("https://launch.stellar.org/#/login");
    login.login("myusername", "mypassword");
    // login success but after that when I execute findElement function is always throw Exception like this 

线程“main”中的异常org.openqa.selenium.InvalidSelectorException:给定的选择器btn btn-default stellar-button ng-binding无效或不会产生WebElement。发生以下错误: InvalidSelectorError:不允许使用复合类名 ....

我也尝试使用Explicit和Implicit Waits,但是没有用,甚至print getCurrentURL()总是“https://launch.stellar.org/#/login”。有没有人知道这个问题的潜在原因或解决方案?

2 个答案:

答案 0 :(得分:0)

您写道,您的登录成功。你怎么会知道这事? 我没有stellar.org的帐户,但如果getCurrentUrl仍然提供" https://launch.stellar.org/#/login",那么我建议你的登录失败。 你试过driver.getPageSource()吗?

您还会收到InvalidSelectorException。我想Exception试图解释自己。你的选择器是错误的(可能是一个被遗忘的关闭括号)或选择器没有导致WebElement。 如果你不能帮助你的例外,它可能是目标 - 旨在发布导致异常的选择器。

答案 1 :(得分:0)

这是一个基于AngularJS的应用程序绕过查找元素可能有点麻烦,除非使用Protractor,否则你最终会得到Long Xpaths和CSS选择器。

这里有一段你的课程重构为我工作。 我注意到你说你正在使用明确的等待,它确实对我有用... 我的建议是在进行自动化之前手动和仔细地研究应用程序的行为。

 public class AutoTest{

public static WebDriver autoDriver;

public AutoTest(){

    autoDriver = new FirefoxDriver();
    autoDriver.get("https://launch.stellar.org/#/login");

}

public void login(String username, String password) throws InterruptedException{

    autoDriver.findElement(By.id("username")).sendKeys(username);
    autoDriver.findElement(By.id("password")).sendKeys(password);
    Thread.sleep(1000);
    autoDriver.findElement(By.xpath("//button[.='Log in']")).click();


}

public void faceBookStellar() throws InterruptedException{

    WebElement facebookButon = new WebDriverWait(autoDriver, 10)
    .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[.='Receive your first stellars on us!']")));
    facebookButon.click();


}

public void quiteBrowser(){

    autoDriver.close();
    autoDriver.quit();
}

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

    AutoTest testObject = new AutoTest();
    testObject.login("autoStellarTester", "password");
    testObject.faceBookStellar();

}

}