问题
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)
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!
回答1:
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)
来源:https://stackoverflow.com/questions/35805049/uploading-photos-to-craigslist-with-python-and-selenium