How to insert images (from your directory) through the use of code

Question: I have a specific image in my directory that I would like to use. I’m trying to change my profile picture but for some reason replit has their own sort of file explorer and i’m not sure how i can use python and selenium to choose that specific image.

Can you send the link to your Repl?

not sure how, but incase you wanted to look at the directory – here it is:

image

What OS are you using? It looks like Linux to me. Click “Other Locations” and browse for the file there.

1 Like

See this guide on how to share your code:

1 Like

how can i do that through code though

You want to browse the files on your computer using your Python code in Replit? I’m afraid that’s not possible since your Repl is on Replit’s servers and your computer is not. Can you provide the link to your Repl as Qwerty suggested?

no, i want to browser the files on my replit project using code lol. and yes let me give it a shot

from os import listdir
files = listdir()
print(files)

i mean yeah that’s to find the specific file/image… but how can i INSERT that file through python? no clue on how to do it when this window pops up

Python generally doesn’t use images. What is the link to your Repl?

@anon40284853 , @QwertyQwerty88 , @not-ethan

here is an example of what im trying to do: https://replit.com/@oblizz/test#main.py

basically when the window pops up, how can i navigate to that specific file in my directory and upload that?

1 Like

Please do not ping users who have not participated in the thread.

2 Likes

The docs seem to have an example:

2 Likes

had a look at the website and came up with this:

# clicking on 'add files'
driver.find_element(By.XPATH, '/html/body/section/div/div[4]/div/div/div/table[2]/tbody/tr/td[1]/div[1]/a[1]/span[2]').send_keys('pic.png')

though it throws an error – if someone can kindly check it out

1 Like

You’re trying to add a file to a span element. Try adding it to the input element:

# clicking on 'add files'
driver.find_element(By.XPATH, '/html/body/section/div/div[4]/div/div/div/table[2]/tbody/tr/td[1]/div[1]/div/input').send_keys('pic.png')
2 Likes

error:

Traceback (most recent call last):
  File "main.py", line 19, in <module>
    driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/form/input[1]').send_keys('pic.png')
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 231, in send_keys
    self._execute(
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: path is not absolute: pic.png
  (Session info: chrome=114.0.5735.106)
Stacktrace:
#0 0x55e886d8c4e3 <unknown>
#1 0x55e886abbc76 <unknown>
#2 0x55e886ac6f32 <unknown>
#3 0x55e886af17f5 <unknown>
#4 0x55e886b17012 <unknown>
#5 0x55e886aedb03 <unknown>
#6 0x55e886b171de <unknown>
#7 0x55e886b2f30e <unknown>
#8 0x55e886b16de3 <unknown>
#9 0x55e886aec2dd <unknown>
#10 0x55e886aed34e <unknown>
#11 0x55e886d4c3e4 <unknown>
#12 0x55e886d503d7 <unknown>
#13 0x55e886d5ab20 <unknown>
#14 0x55e886d51023 <unknown>
#15 0x55e886d1f1aa <unknown>
#16 0x55e886d756b8 <unknown>
#17 0x55e886d75847 <unknown>
#18 0x55e886d85243 <unknown>
#19 0x7f4dde5d0e24 start_thread
1 Like

The error’s pretty clear - the path isn’t absolute. Try this to get the absolute path

import os
os.path.abspath("pic.png")
1 Like

it worked for that specific site, how will it work for a site that doesn’t have a input element?

for example this website: Plupload: UI Widget

here is my code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.by import By

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)

import os
print(os.path.abspath("pic.png"))

driver.get("https://plupload.com/examples")

# clicking on 'add files'
driver.find_element(By.XPATH, '/html/body/section/div/div[4]/div/div/div/table[2]/tbody/tr/td[1]/div[1]/a[1]').send_keys(os.path.abspath("pic.png"))

here is the error:

  File "main.py", line 19, in <module>
    driver.find_element(By.XPATH, '/html/body/section/div/div[4]/div/div/div/table[2]/tbody/tr/td[1]/div[1]/a[1]').send_keys(os.path.abspath("pic.png"))
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 231, in send_keys
    self._execute(
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/home/runner/test/.pythonlibs/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=114.0.5735.106)
Stacktrace:
#0 0x5653ae56f4e3 <unknown>
#1 0x5653ae29eb00 <unknown>
#2 0x5653ae2d552c <unknown>
#3 0x5653ae2d4b0a <unknown>
#4 0x5653ae2fa012 <unknown>
#5 0x5653ae2d0b03 <unknown>
#6 0x5653ae2fa1de <unknown>
#7 0x5653ae31230e <unknown>
#8 0x5653ae2f9de3 <unknown>
#9 0x5653ae2cf2dd <unknown>
#10 0x5653ae2d034e <unknown>
#11 0x5653ae52f3e4 <unknown>
#12 0x5653ae5333d7 <unknown>
#13 0x5653ae53db20 <unknown>
#14 0x5653ae534023 <unknown>
#15 0x5653ae5021aa <unknown>
#16 0x5653ae5586b8 <unknown>
#17 0x5653ae558847 <unknown>
#18 0x5653ae568243 <unknown>
#19 0x7f7c9bfd4e24 start_thread
  1. That’s the same site you were using earlier
  2. That site does have an input element, which is the one I selected in the solution I gave you
  3. If a site really doesn’t have an input element, then you can’t upload files. (Unless they use some other form of file uploads)