Home > Software design >  Selenium Python automation - Cannot find upload button/input type="file"
Selenium Python automation - Cannot find upload button/input type="file"

Time:01-15

I'm trying to locate an element with input type="file", however after researching many different approaches and ways to simply locate an element on the page based on XPath, CSS selector, Tag, etc. The script encounters a NoSuchElementException. The element is in an iFrame.

The exception is:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.jsx-2404389384"}

The code that caused the exception is:

driver.find_element(by=By.CSS_SELECTOR, value='div.jsx-2404389384')

Any ideas would be appreciated, thanks

EDIT: The actual element on the webpage is:

<input type="file" accept="video/*"  style="display: none;">

Sorry the element WAS in an iframe... I've been trying different ways to solve this problem that I must have looked over the iframe part :|

CodePudding user response:

You need to locate that element based on it type, not on it class name since the class name appearing here seems to be a dynamic value.
Try this:

driver.find_element(By.CSS_SELECTOR, "input[type='file'][accept*='video']")

The same with XPath:

driver.find_element(By.XPATH, "//input[@type='file'][contains(accept,'video')]")

Also, I hope you understand that you will need to send the absolute path of uploaded file as a string to this element, like this:

driver.find_element(By.XPATH, "//input[@type='file'][contains(accept,'video')]").send_keys("C:\images\imageupload.exe")

CodePudding user response:

First, your locator uses the wrong HTML tag. div.jsx2404389384 would locate an element with a <div> tag and class=jsx2404389384.

You want to locate an element with an <input> tag, so

driver.find_element(By.CSS_SELECTOR, 'input.jsx-2404389384')

should work, assuming there is only one <input> element with that class. If there are multiple, you would need to use driver.find_elements and find the right one out of the list of <input>s with class=jsx-2404389384.

Second, it does look like that class looks like it might be automatically generated and/or subject to change, so a CSS selector using attribute matching, like

driver.find_element(By.CSS_SELECTOR, input[type='file'][accept*='video'])

is better. That will find tags with type attribute matching 'file' and accept attribute containing 'video'.

For more information, the Selenium docs link to a great resource on CSS selectors. The latter link will give you more information about more advanced and specific CSS selectors, if you need them. For way more, there is the Mozilla developer network.

CodePudding user response:

The element was in an iframe so I just had to switch to the iframe and then the original XPATH, CSS, etc code worked.

I appreciate the answers anyway!

  • Related