0

I'm automating some scripts on Craigslist. I want to upload 2 pictures without the dialogue box opening. Here is a screenshot, the button says "Choose Files": enter image description here

I have referenced the following StackOverflow post: How to upload file ( picture ) with selenium, python

But my code is still not working. Here is my code:

from selenium import webdriver
import os

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()

# Use Classic Uploader
driver.find_element_by_id("classic").click()

# Select the Image
driver.find_elements_by_name("file").send_keys(os.getcwd()+"/Desktop/College Athlete/Logo/Logo.png")

Any tips would be great. I've also tried

find_element_by_css_selector.('input[type="file"]')

...and it still didn't work.

wolfbagel
  • 468
  • 2
  • 11
  • 21
  • 2
    `body.send_keys("this is a test)` you are missing the end quote – kstullich Nov 06 '17 at 23:05
  • @kstullich Didn't fix it. – wolfbagel Nov 06 '17 at 23:41
  • Well what is the error you're getting? – kstullich Nov 06 '17 at 23:42
  • I enter your page follow your code, your code looks correct. Please try to send_keys a hard code string which is an absolute path of a real file on your disk to see upload work or not. if work, it means the file path calculated by os.getcwd()+"/Desktop/College Athlete/Logo/Logo.png" is wrong, you can print out its value before send_keys. – yong Nov 07 '17 at 01:15
  • Possible duplicate of [How to upload file ( picture ) with selenium, python](https://stackoverflow.com/questions/8665072/how-to-upload-file-picture-with-selenium-python) – JeffC Nov 07 '17 at 02:47
  • I found the issue. My path the pictures I wanted to upload was wrong, so I removed "os.getcwd()" and included the absolute path to my files on my Desktop. – wolfbagel Nov 08 '17 at 20:19

1 Answers1

0

I figured out the issue. I did not have the correct path to my pictures and therefore it was failing. I'm now using the absolute path.

I changed the following line:

driver.find_elements_by_name("file").send_keys(os.getcwd()+"/Desktop/College Athlete/Logo/Logo.png")

to this:

element = driver.find_element_by_name("file")
    element.send_keys("/Users/john/Desktop/PycharmProjects/selenium/craigslist/ProfilePicture.png")
wolfbagel
  • 468
  • 2
  • 11
  • 21