【问题标题】:Not able to uplaod file in selenium python [duplicate]无法在 selenium python 中上传文件 [重复]
【发布时间】:2022-01-03 14:25:41
【问题描述】:

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

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

请帮帮我。

提前致谢。

【问题讨论】:

    标签: python selenium-webdriver ui-automation


    【解决方案1】:

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

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

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    你应该检查的CSS:

    input[type='file']
    

    检查步骤:

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

    【讨论】:

      【解决方案2】:
      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 生成它,如果它不能完全回答您想要做的事情,您也可以这样做。

      【讨论】:

      • askjarvis.io/protected/demo.html 的内容最终会出现在帐户墙后面。这是不利的。请修改它对所有人开放。此外,添加您的解决方案为何有效的上下文。两者都将提高答案质量。审查结束。
      猜你喜欢
      • 2022-10-16
      • 1970-01-01
      • 2019-12-26
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2020-11-26
      相关资源
      最近更新 更多