【问题标题】:Selenium Webdriver - Unable to locate button with negative value of topSelenium Webdriver - 无法找到顶部为负值的按钮
【发布时间】: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-&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("//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);

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

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

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

    或者通过添加Webdriver wait-:

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

    确保您已触发弹出窗口的事件。

    【讨论】:

      【解决方案2】:

      通常这些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)
      

      【讨论】:

      • 感谢您的回答,但是每次我们刷新浏览器时都会更改 ID 的这部分“55c3fefdb188d”,所以我无法选择任何永久 ID,有什么方法可以从 javascript 中选择动态 ID?和下面的Jquery在参数列表后返回错误“缺少)” js.executeScript("$('input[type=\'file\']').css('top', 1)");有什么建议吗?
      【解决方案3】:

      对于此类错误,我们应该使用线程睡眠。

      Thread.sleep(1000)
      

      我希望它会起作用。

      【讨论】:

      • 您可能想在此处添加解释和更多细节......
      【解决方案4】:

      你可以试试

      driver.waitUntil(ExpectedCondition.visibilityOfElementLocated(By.xpath(path)), miliseconds);
      

      然后

      driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\a.png");
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2022-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        • 2016-03-02
        • 2016-09-22
        • 2016-08-13
        • 1970-01-01
        相关资源
        最近更新 更多