Phantomjs 浏览器 element.getText() 为空 [英] Phantomjs browser element.getText() is empty
问题描述
我有一个简单的测试,它将 element.getText()
值打印到控制台.如果我使用 ChromeDriver 运行代码,它可以正常工作.元素可见,我也在等待元素可见,并且在 getText()
调用之前测试了 element.isDisplayed()
.但是如果我用 Phantomjs 驱动程序运行它, getText()
的结果是一个空字符串.代码如下:
I have a simple test which prints element.getText()
value to console. If I run the code with ChromeDriver it works without any problem. Element is visible also I am waiting till element is visible and also I test the element.isDisplayed()
before the getText()
call. But if I run it with Phantomjs driver the result of getText()
is an empty string. Here is the code:
public void test() throws InterruptedException {
openPage();
WebElement header = driver.findElement(By.id("header"));
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(header));
System.out.println(header.isDisplayed());
System.out.println(header.getText());
}
这个元素有一个 CSS 转换.
There is a CSS transform on this element.
这是网页 https://tatrytec.eu/,它是 Tatrytec 的主要标题.欧盟
This is the web page https://tatrytec.eu/, and it is the main header Tatrytec.eu
谁能告诉我问题出在哪里?
Can anybody tell me please where could be the problem?
推荐答案
https://phantomjs.org/,该项目现已暂停.chrome 和 phantom 使用不同的渲染引擎,所以现在使用 phantomjs 没有意义,改用 chrome headless
https://phantomjs.org/ , this project is suspended as of now. chrome and phantom uses different rendering enginees so it doesn't make sense to use phantomjs now, use chrome headless instead
https://www.chromium.org/blink
以前像phantomjs这样的无头浏览器曾经使用webkit渲染引擎,但现在chrome内置了无头支持并使用相同的渲染引擎blink
Previously headless browsers like phantomjs used to use webkit rendering engine but now chrome has inbuild headless support and uses same rendering enginee blink
另请注意,w3c 对于非无头 chrome 是默认开启的,但对于无头 chrome 是错误的并且使用 JSONwire 协议,这不会影响测试质量,但仅供参考
Also note w3c is on by default for non headless chrome but for headless chrome it is false and use JSONwire protocol , that doesn't affect quality of test but just for your information
https://stackoverflow.com/a/65841695/6793637
截至 2021 年 2 月
As of Feb , 2021
https://bugs.chromium.org/p/chromedriver/issues/detail?id=1925
Headless chrome 不支持首选项设置
Headless chrome doesn't support preferrences setting
您可以将无头铬用作:
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1400,800");
options.addArguments("disable-gpu")
//options.addArguments("--headless", "--disable-gpu", "--window-size=1400,800","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);
这篇关于Phantomjs 浏览器 element.getText() 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!