1

I am trying to upload a file using python automation. While I try to execute the code below python selenium throws an error. Even I tried waiting for 10 seconds to avoid synchronisation issues.

driver.execute_script('window.open("https://ocr.space/" , "new window")')
Imagepath = r"C:\User\Desktop\banner.png"
field=driver.find_element_by_xpath('//input[@type="file"]')
field.send_keys(Imagepath)

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type="file"]"}

Website url:

https://ocr.space/

HTML snippet:

<div class="span8">
  <input type="file" id="imageFile" class="form-control choose valid">
</div>
Sunil Chaudhary
  • 4,481
  • 3
  • 22
  • 41
Santosh
  • 23
  • 4
  • Why are you opening the site with `window.open()` instead of the standard way? When you open the site in a new tab, you have to switch to that tab before you can interact with it. That's the problem. – JeffC Mar 02 '19 at 15:16
  • @JeffC Rather than criticizing OP's approach you need to ask about OP's usecase. The approach OP is following may have a dependency on other factors which we are not aware of. – undetected Selenium Mar 02 '19 at 17:34
  • @DebanjanB I didn't criticize, I asked a question and explained the problem with using this approach... which accounts for the error he's receiving. – JeffC Mar 02 '19 at 23:34

3 Answers3

2

Changing the code to launch the url with get seems to solve the issue.

from selenium import webdriver


driver = webdriver.Chrome("./chromedriver")

driver.get("https://ocr.space/")
image = r"C:\Users\Thanthu Nair\Desktop\soc360.png"
field=driver.find_element_by_xpath('//input[@type="file"]')
field.send_keys(image)

Also make sure the path provided C:\User\Desktop\banner.png is correct, otherwise you'll get another exception. It is just my assumption that this path might be wrong because usually Desktop folder is inside folder with user's name which is inside the User folder. In this case you've Desktop folder is inside User folder according to the path you've give.

Thanthu
  • 4,399
  • 34
  • 43
1

To solve your problem, simply replace new window with _self in the below line of your code :

driver.execute_script('window.open("https://ocr.space/" , "_self")')

Your code is working fine but the reason for an error is, after running your code it launches browser with two tabs nothing but windows and the page will be launched in the second window so you need to switch to that window before uploading an image.

You can use window handles for switching to that window. Below is the code in Java, you can try doing same using Python :

// Using JavaScriptExecutor to launch the browser
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open(\"https://ocr.space/\" , \"new window\")");
// Fetching window handles and switching to the last window
Set<String> handles = driver.getWindowHandles();
for(String handle : handles) {
    driver.switchTo().window(handle);
}
// Printing window title
System.out.println(driver.getTitle());
// Uploading an image
WebElement field = driver.findElement(By.xpath("//input[@type='file']"));
String imagePath = "some image";
field.sendKeys(imagePath);

If you use window.open() to launch an URL then it will do two things, first it will launch browser with default window then it will open URL in new tab even if you don't provide new window argument in your JavaScript function. You need to switch to a particular window to perform any operations on it if you choose this way.

To avoid an above problem, simply you can use driver.get(URL) or driver.navigate().to(URL) which launches the browser and navigates to a particular URL in the same launched browser window.

If you want to use JavaScriptExecutor only without doing switching, you can pass _self as a second argument to the JavaScript function like below instead of new window which avoids switching and launches an URL in the same window :

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open(\"https://ocr.space/\" , \"_self\")");

System.out.println(driver.getTitle());

WebElement field = driver.findElement(By.xpath("//input[@type='file']"));
String imagePath = "some image";
field.sendKeys(imagePath);

I hope it helps...

Ali
  • 1,689
  • 1
  • 5
  • 12
0

Generally, when the file upload related <input> tag contains the attribute type as file you can invoke send_keys() to populate the relevant text field with a character sequence. However, in your usecase the <input> tag though having type="file" but the class attributes are form-control choose which is as follows:

<input type="file" id="imageFile" class="form-control choose">

So, you may not able to able to send a character sequence invoking send_keys().

In these cases you need to use Auto IT based solutions. You can find a couple of relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • How does this answer the question? The problem is `Unable to locate element`, not that he can't `send_keys()` to the element. – JeffC Mar 02 '19 at 15:14
  • @JeffC Due to you limited visibility once again you have miserably failed to see the bigger picture. Please take some some time to understand the usecases and react accordingly. – undetected Selenium Mar 02 '19 at 17:39
  • ...and again you failed to read and understand the question. How is `.send_keys()` the issue (line 4) when line 3 causes the failure? But clearly that won't stop you from criticizing and insulting others for not understanding the issue. – JeffC Mar 02 '19 at 23:38