无法在 selenium python 中上传文件

Not able to uplaod file in selenium python

我无法从本地上传文件。下面是我用来上传文件的代码:

BrowseElement = driver.find_element(By.CSS_SELECTOR, "span[class='image-icon material-icons-round post_image-icon__37fM5']")
BrowseElement.send_keys("C://Users//deepa//Downloads//deepak_1.png")

以下是我遇到的错误:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

请帮帮我。

提前致谢。

import os
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
driver.find_element_by_name("q").send_keys("selenium python")
driver.find_element_by_name("btnK").click()

# get the current directory
current_dir = os.getcwd()

# get the file path
file_path = os.path.join(current_dir, "test.txt")

# upload the file
driver.driver.find_element_by_css_selector("file-upload").send_keys(file_path)

# close the browser
driver.close()

我使用 https://askjarvis.io/protected/demo.html 生成了它,如果它不能完全回答您想要做的事情,您也可以这样做。

如果您的网页至少有一个类型为文件的输入字段,那么下面的代码应该可以工作

BrowseElement = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
BrowseElement.send_keys("C://Users//deepa//Downloads//deepak_1.png")

如果我们在 HTML DOM 中有 unique 条目,请检查 dev tools (Google chrome)。

Css 你应该检查 :

input[type='file']

检查步骤:

Press F12 in Chrome -> 转到 element 部分 -> 执行 CTRL + F -> 然后粘贴 css and see, if your desired 元素 is getting **highlighted** with1/1` 匹配节点。