【问题标题】:(Python) Upload a file via 'Browse' window Selenium(Python) 通过“浏览”窗口 Selenium 上传文件
【发布时间】:2016-05-06 16:22:34
【问题描述】:

出于测试目的,我需要

  1. 通过“浏览”窗口选择一个文件 (See screenshot here)
  2. 点击“打开”(将文件上传到网站),然后
  3. 点击“上传”。

我如何通过硒做到这一点?因为以下方法不起作用:

# hident2 is the name of "Choose File" element
wd.find_element(By.XPATH("//input[@id='hident2']")).sendKeys("C:\\Users\\file-to-upload.xml"); 

# input.btn.primary is the name of "Upload" button element**
wd.find_element_by_css_selector("input.btn.primary").click()

我收到以下错误:

TypeError: 'str' 对象不可调用

我做错了什么?

【问题讨论】:

    标签: python selenium automation


    【解决方案1】:

    您没有正确使用find_element() 方法。将 By.XPATH 作为单独的参数传递:

    wd.find_element(By.XPATH, "//input[@id='hident2']")
    

    或者,使用直接快捷方式:

    wd.find_element_by_xpath("//input[@id='hident2']")
    

    或者,只需使用“by id”内置定位器:

    wd.find_element_by_id("hident2")
    

    此外,该方法称为 send_keys() 而不是 sendKeys()

    【讨论】:

    • 所以我将这一行改为: wd.find_element_by_xpath("//input[@id='hident2']").sendKeys("C:\\Users\\NAVEH\\PycharmProjects\\ SuiteShare 健全性测试\\subject-scheme.xml");正如你所说,现在我收到以下错误:AttributeError: 'WebElement' object has no attribute 'sendKeys'
    • @NavehMadmon 啊,当然,这是第二期,改用send_keys()
    猜你喜欢
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多