使用 Python 和 Selenium 将照片上传到 Craigslist



在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不会安装到我的计算机上。 所以我希望有人能稍微阐明如何发送预先决定的路径名,所以我可以上传照片。任何帮助不胜感激,谢谢!

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

browser.find_element_by_xpath("//input[@type='file']").send_‌​keys(filepath)

最新更新