简体   繁体   English

如何在Java中使用硒仅加载页面上的某些元素

[英]How do you load only certain elements on a page with selenium in Java

If I use the .get method in Selenium, How do I block all elements except the ones I want to load from this website to make the loading process alot faster? 如果我在Selenium中使用.get方法,如何阻止除我要从该网站加载的元素以外的所有元素,以使加载过程更快?

This is what I have done: 这是我所做的:

System.setProperty("dir/chromedriver"); 
ChromeDriver driver = new ChromeDriver(); 
driver.get("url"); 
driver.findElementByCssSelector(".Name").click(); 

while this works with my desired parameters, The .get and .click methods wait till the page fully loads but this takes too long if I only need 2 elements on the page 虽然这可以与我所需的参数一起使用,但.get和.click方法要等到页面完全加载后,但是如果我只需要页面上的2个元素,这将花费很长时间

I don't think you can do that. 我认为您无法做到。 Selenium will wait for the DOM to be loaded to call the CSS selectors. Selenium将等待DOM加载以调用CSS选择器。 That is, it will wait for the dom onload event before you can access the page. 也就是说,它将在访问页面之前等待dom onload事件。 If you are loading stuff with AJAX , then you cannot guarantee that the page has loaded completely. 如果要使用AJAX加载内容,则不能保证页面已完全加载。

More information here :- http://selenium-python.readthedocs.org/navigating.html 此处提供更多信息: -http : //selenium-python.readthedocs.org/navigating.html

Your answer is mentioned in the first paragraph. 在第一段中提到了您的答案。

You can try to execute a jQuery script . 您可以尝试执行jQuery script For example, if you just want to show a div and hide all the rest, you can use: 例如,如果您只想显示一个div并隐藏其余所有内容,则可以使用:

((JavascriptExecutor) driver).executeScript("$('body > :not(#myDiv)').hide();");
((JavascriptExecutor) driver).executeScript("$('#myDiv').appendTo('body');");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM