简体   繁体   English

Selenium Python 自动化 - 找不到上传按钮/输入类型 =“文件”

[英]Selenium Python automation - Cannot find upload button/input type="file"

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.我试图找到一个输入类型为“文件”的元素,但是在研究了许多不同的方法和方法以根据 XPath、CSS 选择器、标签等简单地定位页面上的元素之后。脚本遇到了 NoSuchElementException。 The element is in an iFrame.该元素位于 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/*" class="jsx-2404389384 " 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:|抱歉,元素在 iframe 中......我一直在尝试不同的方法来解决这个问题,我一定看过 iframe 部分:|

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.您需要根据它的类型定位该元素,而不是它的 class 名称,因为此处出现的 class 名称似乎是一个动态值。
Try this:尝试这个:

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

The same with XPath:与 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")

First, your locator uses the wrong HTML tag.首先,您的定位器使用了错误的 HTML 标签。 div.jsx2404389384 would locate an element with a <div> tag and class=jsx2404389384 . div.jsx2404389384会找到一个带有<div>标签和class=jsx2404389384的元素。

You want to locate an element with an <input> tag, so你想定位一个带有<input>标签的元素,所以

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 .应该工作,假设只有一个<input>元素带有 class。如果有多个,您将需要使用driver.find_elements并从带有class=jsx-2404389384<input>列表中找到正确的一个。

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其次,它看起来确实像 class 看起来它可能是自动生成的和/或可能会发生变化,所以 CSS 选择器使用属性匹配,比如

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' .这将找到type属性匹配'file'accept属性包含'video'标签。

For more information, the Selenium docs link to a great resource on CSS selectors .有关详细信息, Selenium 文档链接到CSS 选择器上的重要资源。 The latter link will give you more information about more advanced and specific CSS selectors, if you need them.如果您需要,后一个链接将为您提供有关更高级和特定的 CSS 选择器的更多信息。 For way more, there is the Mozilla developer.network .更多信息,请访问Mozilla developer.network

The element was in an iframe so I just had to switch to the iframe and then the original XPATH, CSS, etc code worked.该元素在 iframe 中,所以我只需要切换到 iframe,然后原始的 XPATH、CSS 等代码就可以工作了。

I appreciate the answers anyway!无论如何,我很欣赏答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM