【问题标题】:Python selenium: Unable to locate the element (//input[@type='file']')Python selenium:无法定位元素(//input[@type='file']')
【发布时间】:2019-07-24 07:16:09
【问题描述】:

我正在尝试使用 python 自动化上传文件。 当我尝试执行下面的代码时,python selenium 会引发错误。 甚至我尝试等待 10 秒以避免同步问题。

driver.execute_script('window.open("https://ocr.space/" , "new window")')
Imagepath = r"C:\User\Desktop\banner.png"
field=driver.find_element_by_xpath('//input[@type="file"]')
field.send_keys(Imagepath)

NoSuchElementException:消息:没有这样的元素:无法定位 元素:{"method":"xpath","selector":"//input[@type="file"]"}

网址:

https://ocr.space/

HTML sn-p:

<div class="span8">
  <input type="file" id="imageFile" class="form-control choose valid">
</div>

【问题讨论】:

  • 你为什么用window.open()而不是标准方式打开网站?当您在新选项卡中打开站点时,您必须先切换到该选项卡,然后才能与之交互。这就是问题所在。
  • @JeffC 您需要询问 OP 的用例,而不是批评 OP 的方法。 OP 所遵循的方法可能依赖于我们不知道的其他因素。
  • @DebanjanB 我没有批评,我问了一个问题并解释了使用这种方法的问题......这解释了他收到的错误。

标签: python html selenium automation


【解决方案1】:

更改代码以使用get 启动网址似乎可以解决问题。

from selenium import webdriver


driver = webdriver.Chrome("./chromedriver")

driver.get("https://ocr.space/")
image = r"C:\Users\Thanthu Nair\Desktop\soc360.png"
field=driver.find_element_by_xpath('//input[@type="file"]')
field.send_keys(image)

还要确保C:\User\Desktop\banner.png 提供的路径是正确的,否则你会得到另一个异常。这只是我的假设,该路径可能是错误的,因为通常桌面文件夹位于用户名文件夹内,用户名位于用户文件夹内。在这种情况下,根据您提供的路径,您的 Desktop 文件夹位于 User 文件夹中。

【讨论】:

    【解决方案2】:

    要解决您的问题,只需在下面的代码行中将 new window 替换为 _self 即可:

    driver.execute_script('window.open("https://ocr.space/" , "_self")')
    

    您的代码运行良好,但出现错误的原因是,在运行您的代码后,它会启动带有两个选项卡的浏览器,只有窗口,并且该页面将在第二个窗口中启动,因此您需要在上传之前切换到该窗口图片。

    您可以使用窗口句柄切换到该窗口。以下是 Java 中的代码,您可以尝试使用 Python 做同样的事情:

    // Using JavaScriptExecutor to launch the browser
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("window.open(\"https://ocr.space/\" , \"new window\")");
    // Fetching window handles and switching to the last window
    Set<String> handles = driver.getWindowHandles();
    for(String handle : handles) {
        driver.switchTo().window(handle);
    }
    // Printing window title
    System.out.println(driver.getTitle());
    // Uploading an image
    WebElement field = driver.findElement(By.xpath("//input[@type='file']"));
    String imagePath = "some image";
    field.sendKeys(imagePath);
    

    如果你使用window.open() 启动一个 URL,那么它会做两件事,首先它会用默认窗口启动浏览器,然后它会在新标签页中打开 URL,即使你没有在你的 JavaScript 中提供new window 参数功能。如果您选择这种方式,您需要切换到特定窗口对其进行任何操作。

    为避免上述问题,您只需使用driver.get(URL)driver.navigate().to(URL) 即可启动浏览器并在同一个启动的浏览器窗口中导航到特定网址。

    如果您只想使用 JavaScriptExecutor 而不进行切换,您可以将 _self 作为第二个参数传递给 JavaScript 函数,如下所示,而不是 new window,这样可以避免切换并在同一窗口中启动 URL:

    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("window.open(\"https://ocr.space/\" , \"_self\")");
    
    System.out.println(driver.getTitle());
    
    WebElement field = driver.findElement(By.xpath("//input[@type='file']"));
    String imagePath = "some image";
    field.sendKeys(imagePath);
    

    希望对你有帮助……

    【讨论】:

      【解决方案3】:

      一般来说,当文件上传相关的&lt;input&gt;标签包含属性typefile你可以调用send_keys()来填充相关的文本字段,带有一个字符序列。但是,在您的用例中,&lt;input&gt; 标签虽然具有type="file",但class 属性是form-control choose,如下所示:

      <input type="file" id="imageFile" class="form-control choose">
      

      因此,您可能无法发送调用send_keys() 的字符序列。

      在这些情况下,您需要使用基于Auto IT 的解决方案。您可以在以下位置找到一些相关讨论:

      【讨论】:

      • 这如何回答这个问题?问题是Unable to locate element,并不是他不能send_keys()到元素。
      • @JeffC 由于您的能见度有限,您再次悲惨地未能看到更大的图景。请花一些时间来了解用例并做出相应的反应。
      • ...您再次未能阅读和理解该问题。当第 3 行导致失败时,.send_keys() 的问题(第 4 行)如何?但显然这不会阻止你批评和侮辱他人不理解问题。
      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多