Selenium + PhantomJS-页面源中缺少元素 [英] Selenium + PhantomJS - missing elements in Page Source

查看:217
本文介绍了Selenium + PhantomJS-页面源中缺少元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Selenium项目+ Java + Maven,可以在Chrome驱动程序上正常运行. 但是,我一直在尝试使其与PhantomJS一起使用,但是遇到了问题. 适用于Chrome驱动程序的相同代码不适用于PhantomJS.我已经进行了很多调试,并且意识到问题在于PhantomJS加载的页面源是不同的.我尝试查找的ID不存在.

I've got a Selenium project + Java + Maven that runs normally on Chrome driver. However, I've been trying to make it work with PhantomJS, but I've encountered a problem. The same code that works on Chrome driver doesn't work on PhantomJS. I've debugged a lot and realized that the problem is that the page source that gets loaded is different with PhantomJS. The IDs that I try to locate are not present.

使用System.out.println(driver.getPageSource());时,输出为: Chrome驱动程序:长度:19,233 PhantomJS驱动程序:长度:14,965

When using System.out.println(driver.getPageSource());, the output for: Chrome driver: length: 19,233 PhantomJS driver: length: 14,965

我已尝试按照另一篇文章(driver.manage().window().setSize(new Dimension(2000, 1500));)中的建议设置大小,但这也无济于事.

I've tried to set size as someone suggested in another post (driver.manage().window().setSize(new Dimension(2000, 1500));), but that doesn't help either.

我试图等待更长的时间来加载页面,这没有帮助.

I've tried waiting longer for the page to load, didn't help.

我还检查了driver.get()中的链接,它是开头为 http ://的完整URL.

I've also checked the link in driver.get() and it's a full URL with http:// at the beginning.

由于我们需要所有测试都可以在Chrome和PhantomJS上运行,所以我宁愿如果不需要每次编写测试并检查两者上都存在的内容时都需要检查PageSource.

As we're going to need all the tests to run both on Chrome and on PhantomJS, I'd rather if we didn't need to check the PageSource everytime we write a test and look for something that's present on both.

我将不胜感激. :)

推荐答案

PhantomJs最近由于其维护不善而出现了错误.

PhantomJs is having bug these days because of poorly maintenance of it.

您可以将chromedriver本身用于无头工作.

You can use chromedriver itself for headless jobs.

您只需要在chromedriver中传递一个选项,如下所示:-

You just need to pass one option in chromedriver as below:-

    chromeOptions.addArguments("--headless");

完整的代码将显示如下:-

Full code will appear like this :-

System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in/");

希望它会为您提供帮助:)

Hope it will help you :)

这篇关于Selenium + PhantomJS-页面源中缺少元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆