java WebDriver PhantomJS 无法找到元素,但在 Firefox 中工作正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26742014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebDriver PhantomJS Unable to find element, but works fine with Firefox
提问by ucipass
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.
很长一段时间以来,我一直把头撞在墙上,所以我想我会问“专家”,为什么下面的代码在 PhantomJS 上不起作用(输入密码),但在 Firefox 上工作得很好。最令人不安的是,一个字段输入(用户名)是成功的,但第二个根本不起作用。页面加载得很好,我已经包含了测试代码来验证其他组件加载得很好。
See code below:
见下面的代码:
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();
}
}
回答by Artjom B.
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.
PhantomJS 1.x 有元素 ID 的问题。该站点已损坏,因为它password
用于页面上永远不会发生的两个元素。只需将选择器中的 id 替换为元素类型 ( input
) 即可解决。
driver.findElement(By.cssSelector("#pintr > input")).sendKeys("1234");
回答by neo
Try the methods from this link
From my experience with WebDriver, it's usually timing issues. Call the method in above link at the beginning of your code so you can make sure everything loads before you try to find them. Or you can simply use Thread.Sleep with long enough time before finding elements.
根据我使用 WebDriver 的经验,这通常是时间问题。在代码开头调用上面链接中的方法,以便在尝试查找它们之前确保所有内容都已加载。或者您可以在查找元素之前使用足够长的 Thread.Sleep 时间。