【问题标题】:Selenium cannot find form element in google calendar (Python)Selenium 在谷歌日历(Python)中找不到表单元素
【发布时间】:2018-11-16 01:38:59
【问题描述】:

我正在尝试使用 Selenium 和 Python 通过 csv 文件将日历事件导入谷歌日历。我无法选择表单元素将我的文件路径输入谷歌。我尝试通过 xpath、cssselector 和类名查找元素,但每次都会遇到相同的错误:

selenium.common.exceptions.NoSuchElementException: 消息:没有这样的 元素:无法定位元素

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

上面显示的示例 xpath 是直接通过 google chrome 复制的。任何想法为什么我不能让它工作?谢谢!这是元素的图片和 HTML 代码。

【问题讨论】:

    标签: python selenium gcal


    【解决方案1】:

    //form[@jsname="GBqgNb"]//input更改xpath,但如果仍然无法找到,请尝试添加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")
    

    【讨论】:

    • 非常感谢!使用 WebDriverWait 解决了我的问题!
    【解决方案2】:

    好的,所以我自己尝试了一下,发现当您打开该特定 URL 时,它会将您重定向到 google 登录页面,该页面没有您的 XPath 元素。所以你可以做的就是去登录页面找到用户名和密码的表格,然后使用sendkeys()输入你的用户名/密码。然后它应该将您重定向到正确的页面,XPath 将起作用。

    使用此代码:

    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
    

    【讨论】:

    • 非常感谢您的帮助!但是,我仍然无法使用 xpath 找到该元素。当您尝试时,它对您有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2020-07-09
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多