【问题标题】:WebDriver PhantomJS Unable to find element, but works fine with FirefoxWebDriver PhantomJS 无法找到元素,但在 Firefox 上运行良好
【发布时间】:2023-04-09 09:09:01
【问题描述】:

我已经把头撞到墙上很长时间了,所以我想我会问“专家”为什么下面的代码在 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();
}
}

【问题讨论】:

  • 可能是时间问题。尝试在每个 findElement 之前使用 Thread.Sleep(2000) 并观察行为。如果它有效,那么你知道这是时间问题。还有一个方法叫做 WaitForPagetoLoad。您可以在输入元素之前调用它。
  • 好吧,解决了我自己的问题。似乎 css 选择器不适用于我使用 by.xpath 和 .//*[@id='password'] 的 PhantomJS,现在它可以工作了。
  • 谢谢neo,实际上我也尝试过用eclipse非常缓慢地调试代码。仍然不确定为什么 css 选择器不起作用。
  • 但是在尝试访问之前检查文档是否已加载或检查元素是否存在仍然是一个好习惯。 WebDriver 的性能可能会在不同的计算机上发生变化。我遇到过测试在一台电脑上运行良好,但在另一台电脑上由于时间问题而失败的情况。

标签: java selenium selenium-webdriver webdriver phantomjs


【解决方案1】:

PhantomJS 1.x 的元素 ID 存在问题。该站点已损坏,因为它使用password 来表示页面上永远不会发生的两个元素。只需将选择器中的 id 替换为元素类型 (input) 即可解决。

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

【讨论】:

  • 你摇滚先生!我没有给你竖起大拇指的名声,但非常感谢!我没有注意到密码的重复名称。
  • 你可以accept我的回答。此外,制作屏幕截图以查看页面上发生的情况总是很好。
  • 好的,谢谢!我使用屏幕截图查看页面,但我看到的只是空白字段。我想我下次必须在元素 ID 上查找重复项。再次感谢您的帮助!!!
  • 是的,我也看到了空白字段,但我也注意到在其他地方出现了一些文本。当该字段不存在或不可见时,您将收到错误消息。这意味着,当该字段应填写但未填写时,您可能选择了错误的字段。
  • 嗨,如果我有 By.id("userid") 我怎么写你的代码?
【解决方案2】:

Try the methods from this link

根据我使用 WebDriver 的经验,这通常是时间问题。在代码开头调用上面链接中的方法,以便在尝试找到它们之前确保所有内容都已加载。或者您可以简单地使用 Thread.Sleep 并在找到元素之前有足够长的时间。

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2019-12-22
    • 2017-12-07
    • 2016-08-05
    相关资源
    最近更新 更多