4

I'm trying to upload a file to a facebook group with selenium chromedriver.

driver.find_element_by_xpath("//input[@type='file']").send_keys("http://www.peta.org/wp-content/uploads/2013/10/goat_2D00_list_2D00_1.jpg")

But It throws an Exception Like this:

selenium.common.exceptions.WebDriverException: Message: unknown error: path is not absolute:

I'm on Windows 10, Chrome 44.0.2403.130, ChromeDriver 2.16.333243, selenium 2.47.1

So how I can upload images from urls ? (without having to explicitly download it)

3 Answers 3

4

Nope, this way you can only upload files from a local machine:

driver.find_element_by_xpath("//input[@type='file']").send_keys("/Path/to/the/file")

Download the image first, then upload. For instance:

With urllib

import os
import urllib

base_dir = "/Path/to/dir/"
path_to_image = os.path.join(base_dir, "upload.jpg")

urllib.urlretrieve("http://www.peta.org/wp-content/uploads/2013/10/goat_2D00_list_2D00_1.jpg", path_to_image)

driver.find_element_by_xpath("//input[@type='file']").send_keys(path_to_image)

With requests

import os
import requests

base_dir = "/Path/to/dir/"
path_to_image = os.path.join(base_dir, "upload.jpg")

response = requests.get("http://www.peta.org/wp-content/uploads/2013/10/goat_2D00_list_2D00_1.jpg")

if response.status_code == 200:
    f = open(base_dir + path_to_image, 'wb')
    f.write(response.content)
    f.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Sad to hear that's not possible without downloading :( Thanks for your answer. I've edited it to add a requests snippet too, hope u like it !
0

I had same issue while working with C#. Resolved it by copying file into a folder in project, then right click the file and click properties - then change option for copy to output directory to copy always. then you can do this in your steps:

var filepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "foldername\filename");

Comments

-1

Use the full path like "C:\\Users\\Casper\\Desktop\\hello.jpg" instead of "hello.jpg".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.