奇怪的Selenium2没有这样的元素错误

时间:2014-03-14 02:34:14

标签: selenium-webdriver

我有这个HTML代码。

`<div> <i class="icon-plus icon-white"></i>
    <span>                                  
        <input type="file" multiple="multiple" name="uploadFile" value="Attach2" id="1309261001000145" style="visibility:hidden;position:absolute;top:0;left:0">  <input type="button" onclick="fireFileButton('1309261001000145')" value="Attach" name="input"  class="btn_grn btn_sm">
    </span>                                         
 </div>`

div在正常的嵌套div中。第二个是显示的&#39;附加文件,一旦点击,&#34; fireFileButton&#34;函数将单击第一个窗体(隐藏在左上角0,0位置)。然后出现一个弹出窗口,因为类型是&#39; file&#39;选择要上传的文件和要上传的ajax。

我使用Selenium2来模拟文件上传过程。 我使用以下代码:

 WebElement attach = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("//input[name^='input']")));
 attach.click();
 WebElement upload = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.id("1309261001000145")));
 upload.sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");

我也试过直接在第一个元素上使用sendKeys函数。但是对于第一和第二都有没有这样的错误。我使用了By.id,name,cssSelector,xpath但无济于事。

可以使用javascript选择元素。

非常感谢任何建议。

2 个答案:

答案 0 :(得分:0)

By.cssSelector("//input[name^='input']")

选择器不应包含//,因此请将以上内容替换为:

By.cssSelector("input[name='input']")

You can refer this to know more about Selectors.

答案 1 :(得分:0)

首先检查:ID标签。

输入按钮的ID似乎是动态的。所以我们无法通过ID访问。

第二次检查:NAME标记。

我们可以使用,因为它有一个名称。

WebElement attach = driver.findElement(By.Name("input"));

attach.click();

如果没有点击,请使用操作。

Actions action = new Actions(driver);

action.moveToElement(attach).click().perform();

您无法使用sendkeys直接传递值以进行文件上传。

检查此页面以查找我的解决方案,以便通过文件上传访问:How to handle Windows file browse window using selenium webdriver