【问题标题】:selenium file upload without <input type="file"> element没有 <input type="file"> 元素的硒文件上传
【发布时间】:2019-01-24 01:59:47
【问题描述】:

我正在尝试使用 selenium/python 通过 here 上传我的简历,在 Resume/CV Attach 部分下。

当我检查 Attach 元素时,它显示为 &lt;a data-source="attach" href="#"&gt;Attach&lt;/a&gt;.

我对 HTML 不太熟悉,所以我尝试通过 xpath 查找元素,使用 send_keys() 上传文件,但它通过程序运行并且不上传任何内容。没有错误消息。

driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div[3]/form/div[1]/div[10]/div/div[3]/a[1]').send_keys(info.resume)

我可以设法找到网络元素并使用click() 打开上传文件选项,但我希望能够完全上传文件。

好像是输入type="file"时的在线上传示例,我以前用过,效果很好。

【问题讨论】:

    标签: javascript python python-2.7 selenium automation


    【解决方案1】:

    其实有一个文件上传的输入。您可以使用以下代码:

    driver.find_element_by_id('file').send_keys(info.resume)
    

    请注意,所有 3 个文件输入字段(简历、求职信和成绩单的非官方副本)都具有相同的 id 属性 "file",因此您可以按索引选择每个字段:

    driver.find_elements_by_id('file')[0].send_keys(info.resume)
    driver.find_elements_by_id('file')[1].send_keys(info.cover_letter)
    driver.find_elements_by_id('file')[2].send_keys(info.transcript)
    

    【讨论】:

    • 成功了,谢谢!你怎么知道元素 id 是“文件”?当我检查元素时,我似乎无法在 html 上找到它
    • 如果有上传文件的表单,那么肯定应该有&lt;input type="file"&gt;元素。您可以通过driver.find_element_by_xpath('//input[@type="file"]').get_attribute('outerHTML') 来检查它的外观
    • 嗯,我明白了,所以你知道元素 id = 'file' 因为有上传文件的选项?
    • 没有。我只是确定有一个输入节点。我可以从带有get_attribute('outerHTML')的元素的外部HTML中获得的属性(包括@id
    猜你喜欢
    • 2021-11-26
    • 2019-08-22
    • 2016-05-25
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多