使用Python和Selenium将照片上传到Craigslist [英] uploading photos to Craigslist with Python and Selenium

查看:122
本文介绍了使用Python和Selenium将照片上传到Craigslist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows10上使用Firefox3和python3硒: 这个程序很简单.它会直接跳到craigslist的发布新列表"页面,上传多张照片,然后提交.我的问题是我无法控制对话框使用硒导航到正确的文件.

using python3, selenium with firefox on windows10: this program is simple. it jumps directly to craigslists 'post a new listing' page, uploads multiple photos, then submit. the problem im having is that i cannot control a dialog box to navigate to the correct file with 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无法安装到我的计算机上.所以我希望有人可以对如何发送预定的路径名​​有所了解,以便我可以上传照片.任何帮助表示赞赏,谢谢!

Ive been doing some reading and i get the gist that i need to use send_keys() to the 'input file', but im still so new with selenium and programming in general, that i dont totally understand the concept. my idea was to use SendKeys from AutoIt, but i cant even figure out why AutoIt wont install to my computer. so im hoping someone can shed a little light on how to send the pre-decided pathname so i can upload photos. any help is appreciated, thanks!

推荐答案

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

You should not operate with <button> element, but with <input> instead, so use following code:

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

这篇关于使用Python和Selenium将照片上传到Craigslist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆