Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python selenium保存的图像为空_Python_Selenium - Fatal编程技术网

python selenium保存的图像为空

python selenium保存的图像为空,python,selenium,Python,Selenium,我不熟悉python中的selenium模块。我正在尝试加载图像并将其保存到本地。为此,我使用以下代码 driver = webdriver.Firefox() driver.execute_script("window.open()" ) driver.switch_to_window(driver.window_handles[1]) driver.get("http://media.santabanta.com/medium1/cricket/trent%20boult/trent-bou

我不熟悉python中的selenium模块。我正在尝试加载图像并将其保存到本地。为此,我使用以下代码

driver = webdriver.Firefox()
driver.execute_script("window.open()" )
driver.switch_to_window(driver.window_handles[1])
driver.get("http://media.santabanta.com/medium1/cricket/trent%20boult/trent-boult-0a.jpg")
driver.save_screenshot('im1.jpg')

我遇到的问题是保存的图像是空的(0字节)

我的答案无法解决您当前的问题,但如果您只想保存图片,可以避免使用
selenium
,只需使用以下代码:

import urllib

urllib.request.urlretrieve("http://media.santabanta.com/medium1/cricket/trent%20boult/trent-boult-0a.jpg", 'path/to/im1.jpg')

在Python2.X中,应该使用
urllib.urlretrieve()
而不是
urllib.request.urlretrieve()

url是受保护的url吗?我无法通过URL直接访问此图像。这可能解释了文件大小为0 KBs的原因。请添加一些隐式等待并重试。我们也可以使用driver.get_screenshot_作为_文件('/tmp/google.png')@demouser123不,不是。您是否在浏览器中打开它?@santhoshkumar driver.get_screenshot_,因为_文件存在相同的问题。您是否可以添加一些隐式等待和一些睡眠并重试?这个截图对其他网站有用吗?虽然这个方法对我有用,但我不知道我是否应该接受这个答案,因为这个问题是关于硒的