uci*_*ass 5 java selenium webdriver phantomjs selenium-webdriver
我已经把头撞到墙上很长一段时间了,所以我想我会问"专家"为什么下面的代码不能用PhantomJS工作(输入密码),但用Firefox工作得很好.最令人不安的是,一个字段条目(用户名)成功,但第二个根本不起作用.页面加载很好,我已经包含测试代码,以验证其他组件加载得很好.
见下面的代码:
import java.io.File;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
public class login {
public static void main(String[] args) {
WebDriver driver;
Boolean verbose = false; //Change to true to test it with firefox
String phantomPath = "../phantomjs-1.9.8-linux-i686/bin/phantomjs";
String url = "https://www.britishairways.com/travel/redeem/execclub/_gf/en_us";
if (verbose) {
driver = new FirefoxDriver();
}
else{
File file = new File(phantomPath);
String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8";
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
System.setProperty("phantomjs.page.settings.userAgent", userAgent);
driver = new PhantomJSDriver();
}
driver.get(url);
try{
driver.findElement(By.id("membershipNumber")).sendKeys("1234");
System.out.println("ID input successful");
if (driver.findElement(By.id("ecuserlogbutton")).isDisplayed()) {
System.out.println("Login Button is present");
}
//This is where it fails with PhantomJS but work with Firefox
driver.findElement(By.cssSelector("#pintr > #password")).sendKeys("1234");
System.out.println("password input successful");
}
catch (Exception e){
System.out.print(e.getMessage());
}
driver.close();
}
}
Run Code Online (Sandbox Code Playgroud)
PhantomJS 1.x存在元素ID问题.该网站已被破坏,因为它使用password了页面上的两个元素,这些元素永远不会发生.只需用元素类型(input)替换选择器中的id即可解决它.
driver.findElement(By.cssSelector("#pintr > input")).sendKeys("1234");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11730 次 |
| 最近记录: |