Home > database >  How to add images from pc to web
How to add images from pc to web

Time:10-18

I have a problem with simple things.

I am not able to add images from my pc to web. I am on Linux.

    driver = webdriver.Chrome(executable_path="/home/PycharmProjects/Drivers/chromedriver_linux64/chromedriver")
    driver.implicitly_wait(10)
    driver.get("my_web_page")
    
    driver.maximize_window()
    driver.find_element_by_xpath("//*[@id='root']/div[1]/div[2]/form/div[1]/input").send_keys("login")
    driver.find_element_by_xpath("//*[@id='root']/div[1]/div[2]/form/div[2]/input").send_keys("pwd")
    btn = driver.find_element_by_xpath("//*[@id='root']/div[1]/div[2]/form/button")
    btn.click()
    driver.find_element_by_xpath("//*[@id='root']/aside/section[2]/a[2]").click()
    driver.find_element_by_xpath("//*[@id='root']/main/div[2]/div[2]/div[1]/div[2]/a").click()

    driver.execute_script("window.scrollBy(0,925)", "")

    drp_list = driver.find_element_by_xpath("//*[@id='root']/main/div[3]/section[3]/div[2]/div[2]/div/div/div/button/label")
    drp_list.send_keys("/home/Desktop/ct.png")

My console output after run script:

    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

This how this element looks in html:

input accept="image/*" type="file" autocomplete="off" tabindex="-1" style="display: none;">

CodePudding user response:

ElementNotInteractableException

ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the HTML DOM, it is not in a state that can be interacted with.

Reasons & Solutions :

The reason for ElementNotInteractableException to occur can be numerous.

  1. Temporary Overlay of other WebElement over the WebElement of our interest :

In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as folllows:

    WebDriverWait wait2 = new WebDriverWait(driver, 10);
    wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
    driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();

A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
  1. Permanent Overlay of other WebElement over the WebElement of our interest :

If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);

CodePudding user response:

Not working in this way. After clicking the upload button, a window file browser opens up, where I have to select the folder > file and click the Open button on the system dialog window.

But the problem is that on Ubuntu I do not have file_path field where I can provide path to image.

  • Related