我在PhantomJS注册定位webelements时遇到了一些问题。我已经浏览了之前回答的问题,其中给出了 2 种可能的解决方案:
- 同步问题 - 提供隐式等待
- css 选择器在 phantomjs 中不起作用。
我已经尝试了这两种解决方案,但我的代码仍然无法正常工作。法典:
public class Headless_phantomJS {
@Test
public void googlesearch() throws InterruptedException
{
File path=new File("C:/Third party softwares/phantomJS/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.manage().window().maximize();
driver.navigate().to("https://www.google.co.in/");
System.out.println("inside Test");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='lst-ib']")).isEnabled();
driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("lol");
driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]")).click();
}
}
这是您自己的代码块,它在PhantomJS 2.1.1中执行得很好,为了方便起见,进行了一些调整和一些添加的Sysouts:
@Test
public void googlesearch()
{
File path=new File("C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.manage().window().maximize();
driver.navigate().to("https://www.google.co.in/");
System.out.println("inside Test");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("Checking if the Search field is Enabled");
driver.findElement(By.name("q")).isEnabled();
System.out.println("Sending lol to Search field");
driver.findElement(By.name("q")).sendKeys("lol");
System.out.println("Clicking on Google Search button Next");
driver.findElement(By.name("btnG")).click();
System.out.println("Google Search Completed");
}