Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium + Python - After clicking on a button, how to I catch the pop up window (specifically, an upload image pop up)

I'm trying to upload images to Microsoft's http://how-old.net/ API to compare with our age and gender classification algorithm [CVPR AMFG15].

I'm using Selenium and it's pretty easy to navigate to the web-site and click the "Use your own photo" button:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://how-old.net/")

elem=driver.find_element_by_id("uploadFileId")
elem.click()

I tried various solution that I found online:

upload_window=driver.find_element_by_partial_link_text("File")

Or:

driver.SwitchTo().defaultContent();

Nothing seems to work. Again, finding the button and clicking it is very easy, catching the window and uploading an image seems the hard part.

EDIT:

I've also tried the following:

driver = webdriver.Firefox()
driver.get("http://how-old.net/")
file_input=driver.find_element_by_id("uploadBtn")
driver.execute_script("arguments[0].style.visibility='visible';", file_input)
file_input.send_keys('image_name.jpg')

But I get the following exception:

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:

Even though from what I see, the button is visible (see attached print screen).

enter image description here

[CVPR AMFG15] Levi, Gil, and Tal Hassner. "Age and Gender Classification using Convolutional Neural Networks."

like image 479
GilLevi Avatar asked Dec 06 '25 02:12

GilLevi


1 Answers

You cannot control the file upload window with selenium.

The common approach to solve the problem is to avoid it being opened in the first place.

Find the file input and pass the path to the file using send_keys():

file_input = driver.find_element_by_id("uploadBtn")
file_input.send_keys("/absolute/path/to/file")

This would not work "as is" since the file input is hidden, make it visible first:

driver.execute_script("arguments[0].style = {visibility: 'visible'};", file_input)
like image 93
alecxe Avatar answered Dec 08 '25 14:12

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!