1

I am able to do all the basic web actions using Selenium, but I would like to do the following:

Click on a "select image" button, then there comes a pop-up, then I would like to enter some text in #1 and #2, and click #3.

enter image description here

Edition: What I have tried is on kijiji site, after clicking the select image button, I select two images (see picture), then I backward locate these image holder and find the Xpath, then I added the following codes (below), but it doesn't seem to do anything, it gave me some stacktrace message and no error.

imageLoc = "C:\AbsolutePath\IMG_20150620_184908_Edited_sm.jpg"
imageHolderXpath = '(//div[@class="image"])[1]'
imageHolderElement = WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(imageHolderXpath))
imageHolderElement.send_keys(imageLoc)

Output:

Message: 
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver@googlecode.com/components/driver-component.js:10659)
    at FirefoxDriver.prototype.findElement (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver@googlecode.com/components/driver-component.js:10668)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
    at DelayedCommand.prototype.execute/< (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
[Finished in 30.3s]

enter image description here

KubiK888
  • 4,377
  • 14
  • 61
  • 115

1 Answers1

1

It is not possible via selenium, the Upload File dialog is out of selenium's reach.

By all means, you should avoid this popup being opened in the first place. A common solution to this problem is to find the corresponding file input element and send the absolute path to a file to be uploaded. Sample:

element = driver.find_element_by_css_selector("input[type=file]")
element.send_keys("/absolute/path/to/a/file")

Also see related topics:

Well, there are certainly other options, like using AutoIt or Sikuli, but they would really be either unreliable, or complex, slow, platform or browser dependent (or all of it).


Here is a sample working code:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.kijiji.ca/p-post-ad.html?categoryId=36")

driver.find_element_by_link_text("British Columbia").click()
driver.find_element_by_link_text("Cariboo Area").click()
driver.find_element_by_link_text("100 Mile House").click()
driver.find_element_by_id("LocUpdate").click()

driver.find_element_by_css_selector("#ImageUpload [type=file]").send_keys("/Users/user/Downloads/test.jpg")

Which produces:

enter image description here

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Could you tell me know to find the "corresponding file input element"? Does it come from the "select images" button, or from where a supposed picture will show up? And does it have to be using _css_selector? – KubiK888 Jan 11 '16 at 07:15
  • @KubiK888 could not tell without actually having access to the webpage you are working with. It does not have to be a CSS selector - you can choose whatever location technique you like. – alecxe Jan 11 '16 at 14:20
  • Hi I have tried out your suggestion (above), but doesn't seem to work? – KubiK888 Jan 14 '16 at 21:33
  • @KubiK888 what do you mean by does not seem to work? – alecxe Jan 14 '16 at 21:34
  • You see in the second picture, if I manually insert pictures it will show the thumbnails attached. But when I run it using the codes, there is nothing attached, and when I click preview or submit button, no image is shown in the actual post. – KubiK888 Jan 14 '16 at 21:38
  • @KubiK888 okay, I don't think I have something to add. The answer provides a general way people solve issues like this. And I am sure this is the solution, you just need to adapt it. If you would provide a link to the page you are woking with, I might be able to reproduce the problem. – alecxe Jan 14 '16 at 21:41
  • much appreciated of your help, here is the link https://www.kijiji.ca/p-post-ad.html?categoryId=36. And the one I want to do is from the "select image" button. – KubiK888 Jan 14 '16 at 21:47
  • Brilliant! It works. I will study your codes since it's more efficient than using xpath – KubiK888 Jan 14 '16 at 23:03