Home > OS >  How to enter file path?
How to enter file path?

Time:12-04

How can I do to type something in the field of the image below?

I've tried without success:

from threading import local
import pandas as pd
import pyautogui
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager 
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.presence_of_element_located((By.XPATH,"//input[@type='file']"))send_keys("C:/Users/my_user/Downloads/doch.jpeg")

for index, row in df.iterrows():
    actions.send_keys((row["message"]))
    actions.perform()

The only palliative solution was:

pyautogui.write((row["photo"]))
pyautogui.press("enter")

I don't want to use pyautogui as it uses the keyboard command and I can't do anything on the computer while the code is running.

enter image description here

enter image description here

CodePudding user response:

Selenium can't upload files using the Windows select file option, so you'll have to do something else - you might be able to use the send_keys function, i.e.:

elem = driver.find_element(By.XPATH, "//input[@type='file']")
elem.send_keys('C:\\Path\\To\\File')

Note that this may not work, depending on the type of input, and you may be able to instead simulate a drag-and-drop operation if the website supports this.

See How to upload file ( picture ) with selenium, python for more info

CodePudding user response:

For windows path you need double backslashes. Try this:

wait.until(EC.presence_of_element_located((By.XPATH,"//input[@type='file']"))send_keys("C:\\Users\\my_user\\Downloads\\doch.jpeg")
  • Related