【发布时间】:2015-10-30 08:11:37
【问题描述】:
我正在尝试打开弹出对话框,然后输入文件位置以上传文件,但在执行 webdriver 时,下面的按钮不可见。
按钮的顶部参数为负值(-5000px),当鼠标移动时,该值会相应改变。
按钮:
<form><iframe onload="ajax.hideLoader();" name="frame_btnUploadImage55c3fefdb188d" style="display:none;"></iframe><input type="file" id="btnUploadImage55c3fefdb188d" name="btnUploadImage55c3fefdb188d" onchange=";ajax.submit('ClickBlocks\\Web\\UI\\POM\\ImgEditor@imgEditor55c3fefda8290->uploadImage_btnUploadImage55c3fefdb188d', 'frame_btnUploadImage55c3fefdb188d')" size="1" onmouseover=";ajax._getFormByTarget('frame_' + this.id); uploadbutton.initialize('btnUploadImage55c3fefdb188d', 0, 0);" style="position: absolute; width: 60px; top: -5000px; z-index: 1; opacity: 0; left: 445px;" runat="server">+ Add</form>
以下是我尝试过但收到错误“元素当前不可见,因此可能无法与之交互”:
driver.findElement(By.cssSelector("input[id^='btnUploadImage']")).sendKeys("C:\a.png");
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\a.png");
driver.findElement(By.xpath("//input[text()='+ Add']")).sendKeys("C:\a.png");
任何有这种按钮经验的人请帮忙。
更新: 我找到了以下方法:
WebElement 元素 = driver.findElement(By.cssSelector("*[id^=btnUploadImage"));
JavascriptExecutor js = (JavascriptExecutor)驱动程序;
js.executeScript("arguments[0].click();", elem);
【问题讨论】: