I'm trying to upload images to Albums same as directory names. Input field is not available until I click 'Select from Computer' link. If I click, it creates a finder windows to select files, which Selenium can't handle.
I've searched google and StackOverflow, but can't find similar problem faced by others..
google_photos_url = "https://photos.google.com/albums"
print("Opening Google Photos....")
driver.get(google_photos_url)
print("Finding Button to create new album....")
album_link = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.MTmRkb.XhF3Vb")))
print("Creating new album....")
album_link.click()
print("Finding element to enter album name...")
enter_album_name = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "textarea.ajQY2.v3oaBb")))
print("waiting...")
print("Entering album name....")
enter_album_name.send_keys(album_name)
print("Clicking Select photos link....")
out_of_textarea = driver.find_element_by_xpath("//div[@jscontroller ='IZGRkd']")
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',out_of_textarea)
print('Finding Select from computer link....')
select_from_computer = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "span.VfPpkd-vQzf8d")))
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";', select_from_computer)
driver.execute_script("arguments[0].click();", select_from_computer)#This is needed to create the dynamic 'input' field, yet it creates a finder pop up which just piles up.
input_field = driver.find_element_by_xpath("//input[@type = 'file']")
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";', input_field)
print(input_field)
input_field.send_keys("/Volumes/path/to/jpg")
If we could create the input field by executing the script related to the element 'select_from_computer ' in some other way; the problem will be solved.
Here is a solution. First disable finder pop up(see this) with
# disable the OS file picker
driver.execute_script("""
document.addEventListener('click', function(evt) {
if (evt.target.type === 'file')
evt.preventDefault();
}, true)
""")
Then click the element which generates input element. Then send_keys to that element. If you want to send multiple files with the same input element, then create a big string containing all file paths separated by a new line character.(see here) This works for Google Photos. Instead of waiting for specific time to upload images, track the element css of the banner which says 'Uploading'. If it is there, don't do anything and once it is gone(i.e. upload is finished and album is loaded) do next step.(see here) I will be releasing a script on Github soon.