简体   繁体   中英

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?

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

I don't think you can do that. Selenium will wait for the DOM to be loaded to call the CSS selectors. That is, it will wait for the dom onload event before you can access the page. If you are loading stuff with AJAX , then you cannot guarantee that the page has loaded completely.

More information here :- http://selenium-python.readthedocs.org/navigating.html

Your answer is mentioned in the first paragraph.

You can try to execute a jQuery script . For example, if you just want to show a div and hide all the rest, you can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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