I want get captcha image from browser. I have got a url of this picture, but the this picture changes each updated time (url is constant).
Is there any solution to get picture from browser (like 'save picture as' button)?
From the other hand, I think it should be work:
link of the dynamic capcha - link
The problem was solved via screenshot:
browser.save_screenshot('screenshot.png') img = browser.find_element_by_xpath('//*[@id="cryptogram"]') loc = img.location image = cv.LoadImage('screenshot.png', True) out = cv.CreateImage((150,60), image.depth, 3) cv.SetImageROI(image, (loc['x'],loc['y'],150,60)) cv.Resize(image, out) cv.SaveImage('out.jpg', out)
Thanks
We can download images with Selenium webdriver in Python. First of all, we shall identify the image that we want to download with the help of the locators like id, class, xpath, and so on. We shall use the open method for opening the file in write and binary mode (is represented by wb).
Here's a complete example (using google's recaptcha as a target):
import urllib from selenium import webdriver driver = webdriver.Firefox() driver.get('http://www.google.com/recaptcha/demo/recaptcha') # get the image source img = driver.find_element_by_xpath('//div[@id="recaptcha_image"]/img') src = img.get_attribute('src') # download the image urllib.urlretrieve(src, "captcha.png") driver.close()
UPDATE:
The problem with dynamic generated images is that there is a new image generated each time you request it. In that case, you have several options:
take a screenshot
from selenium import webdriver driver = webdriver.Firefox() driver.get('https://moscowsg.megafon.ru/ps/scc/php/cryptographp.php?PHPSESSID=mfc540jkbeme81qjvh5t0v0bnjdr7oc6&ref=114&w=150') driver.save_screenshot("screenshot.png") driver.close()
simulate right click + "Save As". See this thread for more info.
Hope that helps.
It's ok to save a screenshot from the whole page and then cut the image from, but you can also to use the "find" method from "webdriver" to locate the image you want to save, and write the "screenshot_as_png" property like below:
from selenium import webdriver driver = webdriver.Firefox() driver.get('https://www.webpagetest.org/') with open('filename.png', 'wb') as file: file.write(driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[2]/table[1]/tbody/tr/td[1]/a/div').screenshot_as_png)
Sometimes it could get an error because of the scroll, but depending on your necessity, it's a good way to get the image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With