- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Download image with Selenium Python
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). Then capture the screenshot of the element that we desire to capture with the screenshot_as_png method.
Finally, the captured image must be written to the opened file with the write method. Let us make an attempt to download the image of an element having the below html −
with open('Logo.png', 'wb') as file: file.write(driver.find_element_by_xpath('//*[@alt="I"]').screenshot_as_png)
from selenium import webdriver #set chromedriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) #maximize browser driver.maximize_window() #launch URL driver.get("https://www.tutorialspoint.com/index.htm"); #open file in write and binary mode with open('Logo.png', 'wb') as file: #identify image to be captured l = driver.find_element_by_xpath('//*[@alt="Tutorialspoint"]') #write file file.write(l.screenshot_as_png) #close browser driver.quit()
File Logo.png gets created in the project folder.
On opening the file −
- How to download all pdf files with selenium python?
- How do you automatically download a Pdf with Selenium Webdriver in Python?
- How do I download Selenium RC?
- How to Download & Install Selenium WebDriver?
- Find out when a download has completed using Python & Selenium
- How to download image from url in Android?
- How can I download Microsoft WebDriver/Edge Driver to use with Selenium?
- How to use chrome webdriver in Selenium to download files in Python?
- Access to file download dialog in Firefox in Selenium.
- How to click on image in selenium webdriver Python?
- How do I automatically download files from a pop up dialog using selenium-python?
- How to set default download directory in selenium Chrome Capabilities?
- How can Tensorflow be used for transfer learning with TF Hub, to download image net classifier?
- How does one use Glide to download an image into a bitmap?
- How install Selenium Webdriver with Python?