Selenium + Python - after clicking on the button, how can I catch the popup (specifically the boot image popup)
I am trying to upload images to Microsoft's http://how-old.net/ API for comparison with our age and gender classification algorithm [CVPR AMFG15].
I am using Selenium and it is quite easy to go to the website and click the Use My 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 solutions I found on the internet:
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 loading the image seems like the difficult 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 am getting the following exception:
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
Although visible, the button is visible (see attached print screen).
[CVPR AMFG15] Levy, Gil and Tal Hassner. "Age and gender classification using convolutional neural networks".
source to share
You cannot control the file upload window with selenium
.
The general approach to solving the problem is not to open it in the first place.
Find the input file
and pass the file path with send_keys()
:
file_input = driver.find_element_by_id("uploadBtn")
file_input.send_keys("/absolute/path/to/file")
This will not work "as is" since the file input is hidden, first make it visible:
driver.execute_script("arguments[0].style = {visibility: 'visible'};", file_input)
source to share