0

I'm trying to automate tasks in Craigslist using Python and Selenium. Everything was going ok until I had to "Add Image". I wrote code to click the "Add Image" button and it opened up a dialogue box (I think that's what it's called). I want to select 2 images and click the "Open" button but I don't know how to write the code for it. I saw a few examples online but nothing really spoke to me.

Below is a screenshot of the dialogue box and highlighted in blue are the images I'd like to select:

Craiglist Picture

Also, here is my code:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://dallas.craigslist.org/")

# Click on "computer" link
driver.find_element_by_link_text("computer").click()
#Post
driver.find_element_by_link_text("post").click()
#Gig Offered radion button
driver.find_element_by_xpath("/html/body/article/section/form/ul/li[2]/label/span[2]").click()
# I want to hire someone
driver.find_element_by_xpath("/html/body/article/section/form/label[1]").click()
#Please choose a category: Computer Gigs"
driver.find_element_by_xpath("//*[@id='picker']/ul/li[1]/label/span[2]").click()
# Choose the location
driver.find_element_by_xpath("/html/body/article/section/form/ul/li[1]/label/input").click()
# Enter text
# Enter Posting Title
posting = driver.find_element_by_id("PostingTitle")
posting.send_keys("[SEEKING] Parents with Kids who Play Sports. Save Money on College!")
# Enter Location
area = driver.find_element_by_id("GeographicArea")
area.send_keys("Dallas Area")
# Enter Zip Code
zipcode = driver.find_element_by_id("postal_code")
zipcode.send_keys("75001")
# Enter Body
body = driver.find_element_by_id("PostingBody")
body.send_keys("this is a test)

# No Pay
driver.find_element_by_xpath("//*[@id='vol_label']/input").click()
#From Email
fromemail = driver.find_element_by_id("FromEMail")
fromemail.send_keys("foo@gmail.com")

# Confirm Email
confirmemail = driver.find_element_by_id("ConfirmEMail")
confirmemail.send_keys("foo@gmail.com")

# Maps
driver.find_element_by_id("wantamap").click()

# Click Continue
driver.find_element_by_xpath("//*[@id='postingForm']/div/button").click()

# Add Images
driver.find_element_by_id("plupload").click()
wolfbagel
  • 468
  • 2
  • 11
  • 21
  • You need to `.send_keys()` to the `INPUT` element. See this answer: https://stackoverflow.com/a/10472542/2386774 – JeffC Nov 05 '17 at 05:13
  • Hi @JeffC. Thank you for the link. I'm still kind of newbie, when it says find_element_by_id() is that the id of the dialogue box that opens up to upload the picture? – wolfbagel Nov 06 '17 at 20:46
  • Typically a page will have a INPUT button that the user clicks, the File | Open dialog launches, the user chooses a file, and then clicks Open. You can see this on this test page: http://the-internet.herokuapp.com/upload. You would get the "Choose File" INPUT (id: file-upload) and send_keys the file path to that INPUT and then click on the Upload button. The File | Open dialog should never open. – JeffC Nov 06 '17 at 21:04

0 Answers0