【问题标题】:Issue with file upload in selenium webdriver browser ie10selenium webdriver浏览器ie10中的文件上传问题
【发布时间】:2016-01-10 17:54:29
【问题描述】:

在 ie 浏览器中遇到文件上传问题,它在 firefox 和 chrome 中工作正常,但在 ie 中不能正常工作。我使用以下代码

driver.findElement(By.xpath("//html/body/div[1]/div/main/div/form/div[1]/div[1]/ng-form/card/fieldset/div/div[9]/div[2]/div[2]/div/span/ng-form/label/div/span[2]")).sendKeys("/home/FF41/application.ini");

如果我们点击它打开一个新窗口,它没有显示任何错误,而不是sendkeys。所以xpath工作正常,但是为什么它失败了,即使xpath是正确的。

绝对 xpath

//html/body/div[1]/div/main/div/form/div[1]/div[1]/ng-form/card/fieldset/div/div[9]/div[2]/div[2]/div/span/ng-form/label/div/span[2]

没有错误,但文件上传未完成。

相对 xpath

 //*[@id=\"c0-Attachment-8\"] 

不可见元素异常

HTML 代码:-

<input id="c0-Attachment-8" class="targetx-file-input ng-pristine ng-valid ng-touched ng-valid-required" type="file" ng-class="{'ng-touched' : attName !== undefined }" ng-required="fileRequired" ng-disabled="success || attName" onchange="angular.element(this).scope().setFiles(this)" accept="" ng-model="filename" name="c0-Attachment-8"/>

【问题讨论】:

  • 你能提供HTML代码吗?您遇到了哪个错误?
  • 如果使用绝对 xpath,它不会显示任何错误 //html/body/div[1]/div/main/div/form/div[1]/div[1]/ng-form /card/fieldset/div/div[9]/div[2]/div[2]/div/span/ng-form/label/div/span[2] 但是如果我使用相对xpath路径它会抛出错误不可见元素
  • 请编辑问题并将这些内容添加到您的问题中。

标签: java selenium selenium-webdriver automation saucelabs


【解决方案1】:

因为您使用的是 ie10,所以很明显您是在 windows 上工作。

现在 Sendkeys 需要文件的绝对路径,而您已经提到文件的路径,如 linux 目录

你正在使用这样的路径:-

/home/FF41/application.ini

虽然您需要提供类似 windows 模式的路径:-

C:\\Users\\Easy\\Desktop\\testfile.txt

所以试试

driver.findElement(By.xpath("//html/body/div[1]/div/main/div/form/div[1]/div[1]/ng-form/card/fieldset/div/div[9]/div[2]/div[2]/div/span/ng-form/label/div/span[2]")).sendKeys("C:\\Users\\Easy\\Desktop\\application.ini");

以上操作后还需要点击上传按钮

【讨论】:

  • 我为此目的使用 setFileDetector。实际上我正在使用酱实验室来运行我的测试
  • 我从未尝试过使用 setFileDetector.. 尝试使用上面的示例。它应该适合你:)
  • 它在其他网站上工作,但正在测试的网站失败
  • 您能否在元素上手动上传任何图片,然后单击 DOM 在哪个属性上显示您的文件名。如果它已被跟踪,那么您可以使用 javascriptexecutor
【解决方案2】:

尝试使用下面的 xpath :

//input[@id='c0-Attachment-8']

【讨论】:

  • 元素不显示
  • Wait wait = new FluentWait(driver).withTimeout(200, TimeUnit.SECONDS).pollingEvery(2, TimeUnit.SECONDS).ignoring(NoSuchElementException.class, ElementNotVisibleException.class ); WebElement element = (WebElement) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='c0-Attachment-8']"))); element.isDisplayed();这有帮助吗?
【解决方案3】:

另一个最佳选择是使用 SendKeys 类(在 System.windows.form 命名空间中) 它可以与任何浏览器一起使用。 您可以使用以下代码:

        drv = new InternetExplorerDriver("path to IE Driver Server exe");
        drv.Navigate().GoToUrl("your URL");
        drv.FindElement(By.XPath("enter your xpath here")).Click();
        Thread.Sleep(5000);
        SendKeys.SendWait("complete file path to upload");
        SendKeys.SendWait("{ENTER}");
        drv.FindElement(By.XPath("submit button xpath")).Click();

这段代码是用 C# 编写的。让我知道它是否能解决您的问题。

【讨论】:

  • 您使用的是哪个 selenium IE 驱动程序版本?是不是和这个一样:stackoverflow.com/questions/30897718/…
  • 我在酱汁实验室运行它
  • 你能再尝试一件事吗:隔离 sendkeys 方法,例如:IWebElement ele = drv.FindElement("ByXpath"); ele.SendKeys("你的价值");我用这种方法解决了很多错误。
  • 什么是 IWebElement?我是der?
  • 请忽略 IWebElement ,它应该是 WebElement。如果有效请回复?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
  • 2020-10-24
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
相关资源
最近更新 更多