Selenium Webdriver - 无法找到负值为top的按钮

我试图打开弹出对话框,然后输入文件位置上传文件,但执行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-&gt;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( “//输入[@类型= '文件']”))的SendKeys( “C:\ a.png”); . driver.findElement(By.xpath(“// input [text()='Add']”)) . sendKeys(“C:\ a.png”);

任何人都有这种按钮的经验,请帮忙 .

UPDATE: 我找到了如下方法:

WebElement elem = driver.findElement(By.cssSelector(“* [id ^ = btnUploadImage”)); JavascriptExecutor js =(JavascriptExecutor)驱动程序; js.executeScript(“arguments [0] .click();”,elem);

回答(2)

2 years ago

由于元素不可见,您必须添加隐式等待:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

或者通过添加Webdriver等待:

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(element));

确保已触发事件以显示弹出窗口 .

2 years ago

通常这些 input[type='file'] 是不可见的,为了使其可见,您可以使用javascript

# find your element 
input = driver.find_element_by_css_selector("input type="file")
# make it visible with Jquery
script = "$('input[type=\'file\']').css('top', 1)"
# or javascript
# script = "document.getElementById(\"btnUploadImage55c3fefdb188d\").style.top = 1"
driver.execute_script(script)
# after it become visible use send keys
input.send_keys(PATH_TO_FILE)