WebDriver PhantomJS无法找到元素,但可以在Firefox中正常工作 [英] WebDriver PhantomJS Unable to find element, but works fine with Firefox

查看:152
本文介绍了WebDriver PhantomJS无法找到元素,但可以在Firefox中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把头撞到了墙上很长一段时间所以我想我会问专家为什么下面的代码不能用PhantomJS工作(输入密码),但在Firefox上工作得很好。最令人不安的是,一个字段条目(用户名)成功,但第二个条目根本不起作用。页面加载得很好,我已经包含了测试代码来验证其他组件是否正常加载。

I have been banging my head into the wall for a long time now so I thought I would ask the "experts" why the below code would not work (entering password) with PhantomJS but works just fine with Firefox. The most disturbing of all is that one field entry (username) is successful but the second would not work at all. The page loads just fine and I have included test code to verify other components are loaded just fine.

请参阅下面的代码:

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();
}
}


推荐答案

PhantomJS 1.x有元素ID问题。该网站已损坏,因为它对页面上的两个元素使用密码,这些元素永远不会发生。只需用元素类型( input )替换选择器中的id就可以解决它。

PhantomJS 1.x has a problem with element IDs. The site is broken, because it uses password for two elements on the page which should never happen. Simply replacing the id in the selector with the element type (input) solves it.

driver.findElement(By.cssSelector("#pintr > input")).sendKeys("1234");

这篇关于WebDriver PhantomJS无法找到元素,但可以在Firefox中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆