Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium Upload a file via Windows Upload

I read a lot about and everyone advice to don't use the Windows Upload and write directly the path of my file; I even try to use some command found in the forum like:

swicthTo()
switch_to_window()
window_handles

but I didn't find any solution yet. My main problem is that time I have no space to send directly the path of my file (see image below, the space to introduce the path is grey-out) but I have only the option to click in "Browse" and open the Windows Uploader:

enter image description here

Do you know how can I switch to the upload window's Windows and introduce my fie?

I try even in this way:

browse=wait(".//*[@id='fileinput']") #open the window upload
browse.click()
time.sleep(1)


def handle_dialog(element_initiating_dialog, dialog_text_input):
    upload_dialog = driver.switch_to_active_element
    print (upload_dialog)
    upload_dialog.send_keys(dialog_text_input)
    upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits

handle_dialog(browse, "foobar.txt")

I find the windows and when I print I have this object:

We are already in the Location->Details Page <bound method WebDriver.switch_to_active_element of <selenium.webdriver.ie.webdriver.WebDriver (session="3e725bb7-40a7-452a-ad90-9cca1d41296a")>>

But after when I try to do send_keys I receive this error:

Traceback (most recent call last): File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 149, in handle_dialog(browse, "foobar.txt") File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 145, in handle_dialog upload_dialog.send_keys(dialog_text_input) AttributeError: 'function' object has no attribute 'send_keys'

I partially found a solution, I let behave my code as if the window upload is an alert so I do in this way:

browse=wait(".//*[@id='fileinput']")
browse.click()
time.sleep(1)

upload_dialog = driver.switch_to_alert()
print (upload_dialog)

upload_dialog.send_keys("C:\\Users\\carlo.agresta\\Desktop\\V4LabCredentials.csv")

Now my problem is that I can't accept accept and close the window :S any advice?

Thanks so much in advance

like image 788
Carlo 1585 Avatar asked Jan 30 '23 12:01

Carlo 1585


1 Answers

use autoit example code:

import autoit
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

ActionChains(driver).move_to_element( driver.find_element_by_xpath("//div[@class='upload_button']")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "Edit1", "\\file\\path")
autoit.control_click(handle, "Button1")
like image 174
m.elewa Avatar answered Feb 11 '23 12:02

m.elewa