【问题标题】:Getting an ID or other handle for a file chooser dialog box in Selenium在 Selenium 中获取文件选择器对话框的 ID 或其他句柄
【发布时间】:2019-01-17 14:18:31
【问题描述】:

在 SO 上有很多关于如何在 Selenium 中使用文件选择器的帖子,但他们似乎都认为您引用了代表文件选择器对话框的元素,通常是因为您知道元素的 ID。你怎么找到这个?我使用“检查元素”的常规方法不起作用,因为无法在打开文件选择器对话框的情况下打开检查元素。

相关页面是 images.bing.com。当您单击相机(“使用图像搜索”)然后单击浏览时,会出现文件选择器。

这是我尝试过的。我编写了这个函数来获取所有可见元素的列表,以便在单击链接以调出文件选择器后查看哪些元素是可见的,而不是之前。这个过程识别了三个这样的元素,但是当我运行 elem.get_attribute("id")elem.get_property("id") 时,它们都没有 ID。

def get_visible_elements(self):
    result = set()
    for elem in self.driver.find_elements_by_css_selector("*"):
        try:
            if elem.is_displayed():
                result.add(elem)
        except StaleElementReferenceException:
            pass
    return result

【问题讨论】:

  • 你得到了什么结果?
  • 文件选择器对话框是原生元素,Selenium无法与之交互
  • @MosheSlavin, get_visible_elements 返回 788 个元素,其中 3 个在我单击“浏览”之前不可见。这 3 个都没有 ID,我不确定要在它们上运行哪些其他查询。
  • @DebanjanB,我希望不是这样,但可能是这样。 StackOverflow 上还有其他答案,人们可​​以使用send_keys 与这些对话框进行交互。
  • @kuzzooroo send_keys 只能在 type 属性设置为 file 的相关 <input> 标签上调用

标签: python selenium selenium-webdriver


【解决方案1】:

按照in this Selenium tutorial 给出的步骤上传文件,您不是在寻找对话框元素本身,而是在寻找生成它的<input type="file"> HTML 元素。

要找到它,通常您可以检查打开文件选择对话框的按钮。如果没有,检查它附近的东西并查看附近的 HTML。

在本例中,对于 images.bing.com,我检查了显示 "Drag an image here or browse" 的按钮,并在附近找到了应该可以使用的元素:

<input id="sb_fileinput" class="fileinput" type="file" accept="image/gif, image/jpeg, image/png, image/webp">

【讨论】:

  • 谢谢。在您的帮助下,我获得了working code,但需要注意的是您必须自己在对话框中单击取消。
【解决方案2】:

文件选择器是 OS 原生 控件。因此,Selenium 将无法与 文件选择器对话框 框交互,即使通过 Inspect Element 您也无法找到它。 p>

仅当元素为&lt;input&gt; 标记且type 属性设置为file 时,对表示文件选择器对话框的元素的引用才有效,如下所示:

<input type="file" name="myFile" accept="image/jpeg, image/png">

解决方案

要与 OS 原生 文件选择器对话框 框进行交互,您需要寻找基于 AutoIt 的解决方案。

【讨论】:

    【解决方案3】:

    这里有一些基于@mblakesley 回答的代码。当我调用 send_keys 时,我必须使用 Javascript 使相关元素可见,以避免出现 ElementNotInteractableException

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.get("http://www.bing.com/images")
    
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "sbi_b")))
    driver.find_element_by_id("sbi_b").click()
    
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "sb_brtext")))
    driver.find_element_by_id("sb_brtext").click()
    
    fileinput = driver.find_element_by_id('sb_fileinput')
    driver.execute_script(
        'arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',
        fileinput)
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "sb_fileinput")))
    fileinput.send_keys("/sample.jpg")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2012-02-09
      • 1970-01-01
      • 2023-03-07
      • 2021-08-31
      相关资源
      最近更新 更多