选择文件按钮不上传我的图片(Selenium,Python,Mac)

时间:2017-11-06 22:21:07

标签: python python-3.x selenium

我在Craigslist上自动化了一些脚本。我想在没有打开对话框的情况下上传2张图片。这是一个截图,按钮显示"选择文件": enter image description here

我引用了以下StackOverflow帖子:How to upload file ( picture ) with selenium, python

但是我的代码仍无效。这是我的代码:

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

任何提示都会很棒。我也试过了

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

......它仍然没有用。

1 个答案:

答案 0 :(得分:0)

我想出了这个问题。我的照片没有正确的路径,因此失败了。我现在正在使用绝对路径。

我更改了以下行:

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

到此:

element = driver.find_element_by_name("file")
    element.send_keys("/Users/john/Desktop/PycharmProjects/selenium/craigslist/ProfilePicture.png")