PhantomJS无法检索网站的整个html/找不到Web元素



我正试图使用phantomjs进行无头浏览器测试,我注意到简单的命令,如driver.get(By.id("))返回一个元素未发现异常。我确实设法找到了问题的根源。我做了一个driver.getPageSource(),并注意到phantomjs没有检索或"看到"完整的页面html。

下面的代码是我要运行的。我正在试图找到谷歌主页上的搜索框。在浏览器中查看HTML,可以看到搜索框的id是"list -ib"。但是,在执行getPageSource()时,结果中缺少"lst-ib"。这不是什么大问题,因为我仍然可以通过名称访问元素。但是在其他网页上,整个HTML块丢失,这导致整个元素从getPageSource()中被完全省略。这使得测试这些元素变得不可能。

import static org.junit.Assert.*;
import java.io.File;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AccessMessageDataRetentionSettings
{
    public WebDriver driver;
    @Before
    public void setup()
    {
        File f = new File("My path to the phantomjs executable");
        System.setProperty("phantomjs.binary.path", f.getAbsolutePath());
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setJavascriptEnabled(true);
        //caps.setCapability("takesScreenshot", false);
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--ssl-protocol=any", "--ignore-ssl-errors=true", "--web-security=false" });
        driver = new PhantomJSDriver(caps);
        driver.get("http://www.google.com");
        //((JavascriptExecutor) driver).executeScript("var s=window.document.createElement('script'); s.src='path to my javascript file'; window.document.head.appendChild(s);");
        //WebDriverWait wait = new WebDriverWait(driver, 10);
        //driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("lst-ib")));
    }
    @Test
    public void test() 
    {
        System.out.println(driver.getPageSource());
        //driver.findElement(By.xpath("//input[@id = 'lst-ib']"));
        driver.findElement(By.id("lst-ib"));    
    }
    @After
    public void afterTest()
    {
        driver.quit();
    }   
}

我尝试过的事情:将webdriver声明为PhantomJSDriver,设置DesiredCapabilities的不同组合(包括将ssl协议设置为tlsv1),通过javascriptExecutor(似乎没有做任何事情)执行从https://github.com/facebook/react/issues/945建议的javascript shim,并尝试在Selenium中可用的各种等待。

是PhantomJS只是不兼容现代网站,还是我完全错过了什么?

PhantomJS是一个无头浏览器,你可以在firefox, IE和chrome中处理许多选项,这将是不可能的,例如:

<<p> 不支持的特性/strong>

对插件(如Flash)的支持在很久以前就被取消了。主要原因:

  • 纯无头(没有X11)使得不可能有窗口插件由于这些插件的专有性质(二进制blobs),问题和错误很难调试。以下特性,由于PhantomJS的特性,是不相关的:

  • WebGL需要一个支持opengl的系统。由于PhantomJS的目标是成为100%无头和自包含的,这是不可接受的。在Mesa上使用OpenGL仿真可以克服这一限制,但会导致性能下降。

  • 视频和音频需要多种不同的编解码器。

  • CSS 3-D需要一个透视正确的纹理映射实现。

谢谢大家的回答。PhantomJS最终因为太过时和bug太多而无法使用。我最终使用docker容器运行firefox/chrome图像,然后在RemoteWebDriver上运行我的测试。尽管这意味着它们在容器内的实际浏览器上运行,但对我来说它是"无头的",这对于我的目的来说已经足够好了。

最新更新