2

I am trying to import image to the google form. I am failing to pass keys to the element via xpath. It seems it is a hidden element.

I have tried to execute scripts to unhide it, but with no success. Tried this solutions also: How to access hidden file upload field with Selenium WebDriver python

Python-Selenium "input type file" upload

Does anybody have a method to import to google forms via drag and drop file dialog box.

I usually get an error:

NoSuchElementException: no such element: Unable to locate element:

I am sending some pseudocode:

from selenium import webdriver


browser = webdriver.Chrome()




browser.get('ANY GOOGLE FORM URL')


browser.find_element_by_xpath('//*[@id="mG61Hd"]/div/div[2]/div[2]/div[5]/div/div[3]/span/span').click()


fileinput = browser.find_element_by_xpath("//div[contains(@class,'Nf-Er-Xr')]")
fileinput.send_keys('some image path')
Hari
  • 718
  • 3
  • 9
  • 30
bobo012
  • 23
  • 2
  • Try adding a time.sleep() after browser.get() - sometimes the webpages take a long time to load. – The BrownBatman Jun 14 '19 at 11:05
  • Tried that, it is not a problem. The problem is in a fileinput part. It just cant find the element, by xpath or id. It is somehow hidden, and i quess i need to execute some kind of script to unhide it. But i am stuck on that, nothing works – bobo012 Jun 14 '19 at 11:13

1 Answers1

1

On clicking the ADD FILE button in Google Forms, it opens up a pop-up, which is an iframe. An iframe is basically an HTML page embedded into another one. In order to anything with it, you have to tell Selenium to switch the context to that iframe.

Check this SO question which explains how you'd go about switching context to an iframe. For your specific case, i.e. Google Forms, the code would be ―

iframe = driver.find_element_by_class_name('picker-frame')
driver.switch_to.frame(iframe)
input_field = driver.find_element_by_xpath('//input[@type="file"]')
Sumit Ghosh
  • 1,033
  • 10
  • 29
  • 1
    This works beautifully. I didnt know i need to approach it as a new frame. Thank you very much SkullTech! – bobo012 Jun 14 '19 at 15:08