我正在运行以下代码而不是按预期拍摄屏幕
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get("https://app.wisemapping.com/c/maps/579829/public")
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#workspaceContainer > svg > [preserveAspectRatio]")))
]
driver.save_screenshot('screen.png')
driver.quit()
但是当我使用phantomjs时它失败了
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.22564.0 Safari/537.36')
driver = webdriver.PhantomJS(desired_capabilities=dcap,service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any','--web-security=false'])
driver.get("https://app.wisemapping.com/c/maps/579829/public")
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#workspaceContainer > svg > [preserveAspectRatio]")))
]
driver.save_screenshot('screen.png')
driver.quit()
它给出的错误是
Traceback (most recent call last):
File "C:/Users/HelloWorld.py", line 24, in <module>
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#workspaceContainer > svg > [preserveAspectRatio]")))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Screenshot: available via screen
如果我等待&amp;尝试除了块之外的屏幕截图,而不是屏幕截图,但是在等待状态下的元素没有到来。即使我将超时增加到30秒也不起作用
我正在使用Selenium 3.4和python 2.7.13,phantomjs 2.1
答案 0 :(得分:0)
当driver.get(..)没有开始加载网页内容时,我与phatnomjs有类似的问题。特别是当目标站点自行执行时,window.location.reload()
幻像通常在加载新页面时停止。
使用phantomjs进行解决方法
在我的节点实现中,这看起来像这样。我认为python API类似。
//Setting the pageload timeout
browser.manage().timeouts().pageLoadTimeout(10000);
...
driver.get(url).then(function () {
ok();
}, function () {
//We Retry it a second time
driver.get(url).then(function () {
ok();
}, function (err) {
nok(err);
});
});
替代phantomjs
有关幻影生命周期的一些信息:https://github.com/ariya/phantomjs/issues/15105
现在可以切换到Chrome Headless,这将是webdriver / phantomjs的真正替代品。
使用最新的chromedriver / chrome(抱歉。还有一些js代码而不是python),无头模式下启动chrome很容易:
var Options = require('selenium-webdriver/chrome').Options;
var options = new Options();
options.addArguments('headless');
options.addArguments('disable-gpu');
options.addArguments('window-size=1200,1100');
browser = builder.forBrowser('chrome').setChromeOptions(options).build();