Home > Net >  How can I open a JPG image on my computer using selenium with python
How can I open a JPG image on my computer using selenium with python

Time:07-12

I want to import an organizational logo using Selenium with Python.

I have to click on a link that will open up your file explorer and then I want to be able to find the image using Selenium.

This is what I have so far assuming that I have already made it to the website successfully.

input = driver.find_element(
    By.ID,
    "linkConfigurelogo").click()
input.driver.find_element(
    By.ID, "OrganizationLogo").send_keys(
    "C://filepath//Documents//image.jpg")

CodePudding user response:

More details about your specific project might be necessary to fully answer this question, but this might be a good start.

Firstly, send_keys will send the actual keystrokes of whatever you pass to the element. This code is the equivalent of me clicking on the image element and then typing into my keyboard "C://filepath//Documents..." Unless the website is built to respond to these keystrokes, it doesn't do anything.

To download an image to your computer, you generally need to isolate the src attribute for the image in question. This can be done in selenium:

img = driver.find_element(By.ID, "linkConfigurelogo").click()
src = img.get_attribute('src')

Once you have the source, there exist a few ways to download the image:

import urllib
urllib.urlretrieve(src, "captcha.png")

See this post, this article, and this tutorial

  • Related