1

I am attempting to import calendar events to google calendar through a csv file using Selenium and Python. I am unable to select the form element to input my file path into google. I have tried finding the element by xpath, cssselector and class name and I get the same error every time:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

fileElem = browser.find_element_by_xpath('//*[@id="YPCqFe"]/div/div/div/div[1]/div[1]/div/form')

The example xpath shown above was copied directly through google chrome. Any ideas why I can't get this to work? Thanks! Here's the picture of the element and the HTML code.

screenshot

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
Luke O'Malley
  • 23
  • 1
  • 4

2 Answers2

0

Ok, so I tried it myself and found out that when you open that specific URL, it redirects you to the google sign in page, which doesn't have an element with your XPath. So what you could do is just go to the sign in page and find the forms for username and password, then use sendkeys() to type in your username/password. Then it should redirect you to the right page and the XPath will work.

Use this code:

from selenium import webdriver
import time

d = webdriver.Chrome("executable file path")

d.get("https://calendar.google.com/calendar/r/settings/export")
d.find_element_by_xpath('//*[@id="identifierId"]').send_keys("your email")
d.find_element_by_xpath('//*[@id="identifierNext"]').click() # Next button
time.sleep(0.5) # Sometimes the time it takes to load the next page will cause the next line to fail
d.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys("your password")
d.find_element_by_xpath('//*[@id="passwordNext"]').click() # Next button
d.find_element_by_xpath('//*[@id="YPCqFe"]/div/div/div/div[1]/div[1]/div/form/label') #Now you have the proper element
  • Thanks a lot for your help! However, I was still unable to locate the element using the xpath. Did that work for you when you tried it? – Luke O'Malley Nov 16 '18 at 19:01
0

change the xpath with //form[@jsname="GBqgNb"]//input but if still unable to locate, try add WebDriverWait()

# wait 5 second
fileElem = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH , '//form[@jsname="GBqgNb"]//input')))
fileElem.send_keys("/path/to/file")
ewwink
  • 18,382
  • 2
  • 44
  • 54