Search code examples
pythonseleniumgoogle-image-search

What is Google Image's id or class that corresponds to the box that lets you drag and drop images?


I am using Python Selenium to make some sort of Python Console version of Google Images. I already got the part where it opens up and clicks the camera icon. Unfortunately, I don't know what the id or class is for the box that lets you drag in images, as when I try to use an Id from what appears to be the box it says "Element not interactable" the code so far:

from selenium import webdriver
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get("https://images.google.com")

print("Googlen't Images")
image_query = input("Enter path where image is: ")

cameraicon = driver.find_element_by_class_name("BwoPOe")
cameraicon.click()
time.sleep(2)
box = driver.find_element_by_id("dRSWfb") #this is the one that gives "element not interactable" error
box.send_keys(image_query)

Can anyone help?


Solution

  • First: error gives line with send_keys(), not find_... - so your comment in code is misleading.

    Problem is that "dRSWfb is a <div> and you can't send keys to <div>. Inside this <div> is <input> which you should get and send keys.

    This <input> has id Ycyxxc

    box = driver.find_element_by_id("Ycyxxc")
    box.send_keys(image_query)
    

    I don't know how to drag'n'drop in Selenium (if it is even possible) but DevTools in Firefox shows events dragover and drop for <div> with id QDMvGf


    EDIT: to send local file you can use button Browse on second tab instead of drag'n'drop

    enter image description here

    which you can access using id awyMjb

    box = driver.find_element_by_id("awyMjb")
    box.send_keys(image_query)
    

    Minimal working code

    from selenium import webdriver
    import time
    
    print("Googlen't Images")
    image_query = input("Enter path where image is: ")
    
    driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
    #driver = webdriver.Firefox(executable_path='/home/furas/bin/geckodriver')
    
    driver.get("https://images.google.com")
    
    cameraicon = driver.find_element_by_class_name("BwoPOe")
    cameraicon.click()
    time.sleep(1)
    
    # send word or url on first tab
    #box = driver.find_element_by_id("Ycyxxc")
    #box.send_keys(image_query)
    
    # send local file on second tab
    box = driver.find_element_by_id("awyMjb")
    box.send_keys(image_query)