2

i am currently using selenium with python and my webdriver is firefox i tried the click event but it doesn't work

website = www.cloudsightapi.com/api

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
from selenium.webdriver.common.action_chains import ActionChains

import os
driver = webdriver.Firefox()
driver.get("http://cloudsightapi.com/api")

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget")))
element.click()

Please help !!

Rahul Rao
  • 91
  • 2
  • 8

1 Answers1

6

Clicking via javascript worked for me:

element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget")))
driver.execute_script("arguments[0].click();", element)

Now, the other problem is that clicking that element would only get you into more troubles. There will a file upload popup being opened. And, the problem is, you cannot control it via selenium.

A common way to approach the problem is to find the file input and set it's value to the absolute path to the file you want to upload, see:

In your case, the input is hidden, make it visible and send the path to it:

element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input.dz-hidden-input[type=file]")))

# make the input visible
driver.execute_script("arguments[0].style = {};", element)
element.send_keys("/absolute/path/to/image.jpg")
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thanks for your help ! send_keys() are not working is there any way to drag and drop the image from local computer ? – Rahul Rao Feb 28 '16 at 14:54
  • @RahulRao no way to drag and drop to the browser with selenium only. About send_keys - what do you mean by not working? (worked for me for a sample jpg image) Thanks. – alecxe Feb 28 '16 at 14:54
  • i mean in my case send_keys() are not working as you mentioned that "input" is hidden , which i can't change ! what you suggest now ? – Rahul Rao Feb 28 '16 at 19:34
  • @RahulRao I've provided a way to make the input visible and then send the keys - works for me. Have you tried that? Thanks. – alecxe Feb 28 '16 at 20:46
  • yeahh ! i tried but didn't work for me can you share your output ! – Rahul Rao Feb 29 '16 at 09:03
  • @RahulRao sure, let's start with what do you mean by "did not work"? – alecxe Feb 29 '16 at 14:04
  • `ActionChains(browser).move_to_element(buy_button).click().perform()` didn't click but your javascript code did.. Thanks! – eugene Aug 13 '16 at 01:30
  • 1
    @eugene glad to see it helped. But please be aware what is the [difference between the regular click and "javascript" click](http://stackoverflow.com/questions/34562061/webdriver-click-vs-javascript-click). – alecxe Aug 13 '16 at 02:53