我想截取引导模式中元素的截图。经过一番挣扎,我终于想出了这样一个代码:
driver.get('https://enlinea.sunedu.gob.pe/')
driver.find_element_by_xpath('//div[contains(@class, "img_publica")]').click()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'modalConstancia')))
driver.find_element_by_xpath('//div[contains(@id, "modalConstancia")]').click()
active_element = driver.switch_to.active_element
active_element.find_elements_by_id('doc')[0].send_keys(graduate.id)
# Can't take this screenshot
active_element.find_elements_by_id('captchaImg')[0].screenshot_as_png('test.png')
错误是:
^{pr2}$经过一番调试,我发现元素没有宽度和高度:
(Pdb) active_element.find_elements_by_id('captchaImg')[0].rect
{'height': 0, 'width': 0, 'x': 0, 'y': 0}
(Pdb) active_element.find_elements_by_id('captchaImg')[0].size
{'height': 0, 'width': 0}
我想这就是失败的原因。有办法绕过这个问题吗?在
步骤如下:
如果我检查浏览器中的元素(保存CAPTCHA图像的span
)我可以看到它实际上是100x50:
好的,我已经知道为什么你总是得到
Cannot take screenshot with 0 width.
错误。原因是页面上有多个验证码,使用非特定的选择器会给您一个隐藏的验证码图像(可能在另一个模式窗口下)。所以增加特异性会给你正确的图像。在代码如下:
结果:
相关问题 更多 >
编程相关推荐