4

在 windows10 上使用 python3、selenium 和 firefox:这个程序很简单。它直接跳转到 craigslists 的“发布新列表”页面,上传多张照片,然后提交。我遇到的问题是我无法控制对话框以使用 selenium 导航到正确的文件。

browser = webdriver.Firefox()
browser.get('https://post.craigslist.org/k/lPbhT6Lh5RGBKb-uS1zr0g/g2NjN?lang=en&cc=us&s=editimage')
#opens to craigslists 'Upload/Edit Images' page

add_imgs_btn = browser.find_element_by_id('plupload')
#find the 'add images' button

add_imgs_btn.click()
#clicks the button which opens the dialog box, which is not operable from selenium
add_imgs_btn.send_keys(filepath)

我一直在做一些阅读,我得到了我需要使用 send_keys() 到“输入文件”的要点,但我对硒和编程来说仍然很新,我不完全理解这个概念。我的想法是使用 AutoIt 的 SendKeys,但我什至无法弄清楚为什么 AutoIt 无法安装到我的计算机上。所以我希望有人能对如何发送预先确定的路径名​​有所了解,这样我就可以上传照片了。任何帮助表示赞赏,谢谢!

4

1 回答 1

5

您不应该使用<button>元素操作,而是使用<input>,因此请使用以下代码:

browser.find_element_by_xpath("//input[@type='file']").send_‌​keys(filepath)
于 2016-03-04T22:14:30.267 回答