我正在尝试使用Selenium获取页面源代码,代码是通用SOP。它适用于Baidu.com和example.com。但是当涉及到我实际需要的网址时,我得到了空的page.and,源代码只显示空标签,如下面的代码。我是不是漏掉了什么?
我试图添加更多的选项参数,但似乎没有什么帮助
WebDriver驱动程序;
System.setProperty("webdriver.chrome.driver", "E:\\applications\\ChromeDriver\\chromedriver_win32 (2)//chromedriver.exe");
// 实例化一个WebDriver的对象 作用:启动谷歌浏览器
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
String pageSource = driver.getPageSource();
String title = driver.getTitle();
System.out.println("==========="+title+"==============");
System.out.println(Jsoup.parse(pageSource));
我希望URL的解析页面来源,以便我可以获得我需要的信息。但我被困在这里了。
发布于 2019-06-17 15:18:20
首先,这肯定是一个兼容性问题。这主要是因为selenium-它已经经过了大量的开发,因此,关于版本兼容性的问题数不胜数。这就是我如何处理这个问题。
我选择了火狐浏览器来驱动,版本是67.0(64 bit)
.Cos,正如@Adi Ohana提到的,Chrome会响应一个空白的结果。我在3.X版本中使用Selenium。要使用Selenium 3.X,我在pom.xml中添加以下代码:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.59</version> <!-- this version context matters -->
</dependency>
注意,这是你需要添加到你的pom.xml.otherwise中的<artifactId>selenium-server</artifactId>
,你可能会得到一些意外的错误。
完成这些工作后,你需要一个名为geckodriver.Firefox的合适的driver.the驱动程序。我使用的是v0.24.0版本,它是一个比.jar更大的.exe文件,这样你就可以在编程中通过java代码来指定它,如下所示:
System.setProperty("webdriver.gecko.driver","E:\\applications\\GeckoDriver-v0.24.0-win64\\geckodriver.exe"); // 0.24.0 the 2nd param is the location of geckodriver.exe in your local computer
然后,发送对URL.and的请求,因为主体内容是由另一个AJAX请求加载的。您需要等待几秒钟,Selenium才能做到这一点。
Thread.sleep(5000); // this is the easyest way, may not the best though.
结论:我得到了我期望的原始源代码,但我并没有解决为什么googleDriver不能像预期的那样工作,我可能会把这个留给进一步的挖掘。
总结一下: Firefox 67.0 geckodriver v0.24.0由java-code Selenium 3.X add by xml-code指定
谢谢你们,这真的很有帮助。就像这个社区
附言:我是个新手,刚开始使用stackoverflow.still……
https://stackoverflow.com/questions/56535537
复制相似问题