gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:42:39 25 4
gpt4 key购买 nike

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

下面的代码是我要运行的代码。我试图在 Google 主页上找到搜索框。在浏览器中查看 HTML,您可以看到搜索框的 ID 是“lst-ib”。但是,在执行 getPageSource() 时,结果中缺少“lst-ib”。这没什么大不了的,因为我仍然可以按名称访问该元素。但是在其他网页上,整个 HTML block 都丢失了,这导致整个元素从 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 协议(protocol)设置为 tlsv1),执行 https://github.com/facebook/react/issues/945 建议的 javascript shim通过 javascriptExecutor(它似乎什么也没做),并尝试 Selenium 中可用的各种等待。

PhantomJS 只是与现代网站不兼容,还是我完全遗漏了什么?

最佳答案

PhantomJS 是 headless 浏览器,因此您可以在 firefox、IE 和 chrome 中处理的许多选项是不可能的,例如:

不支持的功能

很久以前就放弃了对插件(例如 Flash)的支持。主要原因:

  • Pure headless(没有 X11)使得不可能有窗口插件由于此类插件(二进制 blob)的专有性质,问题和错误很难调试由于 PhantomJS 的性质,以下功能无关紧要:

  • WebGL 需要支持 OpenGL 的系统。由于 PhantomJS 的目标是成为 100% headless 和独立的,这是 Not Acceptable 。通过 Mesa 使用 OpenGL 模拟可以克服限制,但性能会降低。

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

  • CSS 3-D 需要透视正确的纹理映射实现。它无法在不影响性能的情况下实现。

关于java - PhantomJS 无法检索网站的整个 html/找不到网页元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com