因为Webdriver在进入下一行之前等待整个页面加载,我认为禁用图像,css和javascript会加快速度.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
def disableImages(self):
## get the Firefox profile object
firefoxProfile = FirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
'false')
## Set the modified profile while creating the browser object
self.browserHandle = webdriver.Firefox(firefoxProfile)
Run Code Online (Sandbox Code Playgroud)
我从stackoverflow获取代码不希望加载图像和使用Python在Selenium WebDriver测试中在Firefox上渲染CSS
但是当我补充说
driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com/")
Run Code Online (Sandbox Code Playgroud)
到最后,它仍然加载图像:/
使用FirefoxDriver编写测试时
我发现由于javascript和css被执行,页面的加载速度非常慢.无论如何要禁用它吗?有可能甚至安装Noscript插件到配置文件?
另外,sendKeys()实际上输出了文本.但是,对于长文本来说这很慢,无论如何要立即在输入框中键入所有字符串?
将下面的代码禁用加载火狐硒webdriver的页面上的样式表和图像:
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
driver = webdriver.Firefox(firefox_profile)
driver.get('http://www.stackoverflow.com/')
driver.close()
Run Code Online (Sandbox Code Playgroud)
它适用于stackoverflow.com,facebook.com,yahoo.com ......但有趣的是不适用于谷歌搜索; 只有Google徽标消失且其样式表仍然存在.
如果您尝试使用以下链接http://google.com/search?q=nelson+mandela,您将获得:

而预期的结果应该是这样的(没有样式表+没有图片):

如何在selenium中只加载网页中的html?
我只需要请求页面的 html,不需要 css 和 javascript。
Selenium在继续执行新页面之前等待异步资源调用.
防爆.
<script src="https://apis.google.com/js/platform.js" async defer></script>
Run Code Online (Sandbox Code Playgroud)
在具有许多外部api的网站上(例如来自G +,Facebook和Twitter的Google Analytics和共享按钮).Selenium花费更多时间等待异步调用,而不是运行测试.
反正是否有禁用此行为,以便selenium不等待异步外部api调用?
我想在使用Selenium时在Firefox中禁用图像.它应该是firefox中首选项的简单更新,它在Selenium Python中禁用图像的说明中有记录
但是,当我运行时,图像显示,当我输入about:config时,值permissions.default.image仍为1,而不是2,我尝试将其设置为.
我的代码(用Python编写)是:
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("permissions.default.image", 2)
driver = webdriver.Firefox(firefox_profile)
driver.get(web_address)
Run Code Online (Sandbox Code Playgroud)
作为参考,此代码适用于对首选项的另一个更改,例如使用该行关闭csv文件firefox_profile.set_preference("permissions.default.stylesheet",2).我可以在csv设置和图像之间区分的唯一区别是,该行permissions.default.image已经存在于about:config(即没有我设置它),但是行permissions.default.stylesheet没有....似乎我可以使用我想要的值添加新行,但不能更改现有的行(或者在输入我的值后,它会被Selenium覆盖).
selenium ×5
python ×4
css ×2
firefox ×2
javascript ×2
html ×1
java ×1
unit-testing ×1
webdriver ×1