ProfilesIni profile = new ProfilesIni(); FirefoxProfile ffprofile = profile.getProfile("default");//using firefox default profile ffprofile.setPreference("permissions.default.image", 2); // this make ff to block web page images WebDriver ff = new FirefoxDriver(ffprofile); // executing firefox with specified profile ff.navigate().to("www.google.com"); // loading web page //codes for changing image blocking ???????????
加载某些网页后如何更改图像遮挡?
可以通过开发工具栏CLI在运行中修改首选项,但与加载图像相比,它可能会带来更高的开销。这是Python示例:
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains, Keys ff = webdriver.Firefox() ff.get('http//<URL>') ac = ActionChains(ff) # SHIFT+F2 opens dev toolbar ac.key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform() # command to disable images ac.send_keys('pref set permissions.default.image 2').perform() ac.send_keys(Keys.ENTER).perform() # command to disable flash ac.send_keys('pref set plugin.state.flash 0').perform() ac.send_keys(Keys.ENTER).perform() # disable dev toolbar ac.key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform() ac.key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform() # reload the page to confirm there are no images or flash ff.refresh()