如何在selenium webdriver中识别隐藏的文件元素

时间:2014-03-13 14:41:24

标签: javascript selenium selenium-webdriver

我正在尝试自动执行文件上传功能,但webdriver无法识别文件对象。事情就是这样:

  1. 文件对象位于模态框中(xpath是模态框的// * [@ id ='modalBoxBody'] / div [1])。文件对象的类型和名称分别为file和url。
  2. 当我看到html内容时,有两个具有相同属性的对象。其中一个是可见的,另一个是看不见的。但他们所属的等级制度是不同的。所以我使用了元素可见的层次结构。
  3. 以下是我的代码。我已经尝试了stackoverflow中提供的所有可能的解决方案(尽可能多的搜索),但没有任何效果。注释掉的部分意味着它们也经过了尝试而失败了。

    wbdv.findElement(By.xpath("//*[@id='left-container']/div[4]/ul/li/ul/li[2]/a")).click();
    wbdv.switchTo().activeElement();
    
    System.out.println(wbdv.findElement(By.xpath("//*[@id='modalBoxBody']/div[1]")).isDisplayed()); **//This returns true**
    
    List<WebElement> we = wbdv.findElement(By.xpath("//*[@id='modalBoxBody']/div[1]")).findElement(By.className("modalBoxBodyContent")).findElements(By.name("url")); **//There is only one element named url in this hierarchy**
    System.out.println(we.isEmpty()); //This returns false meaning it got the element named url
    
    //((JavascriptExecutor) wbdv).executeScript("document.getElementsByName('url')[0].style.display='block';"); **//This didn't work**
    for(WebElement ele: we){
    String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
            ((JavascriptExecutor) wbdv).executeScript(js, ele);
    
            System.out.println(ele.isDisplayed()); **//This returns FALSE**
            System.out.println(ele.isEnabled()); **//This returns TRUE**
            System.out.println(ele.isSelected()); **//This returns FALSE**
            ele.click(); **//This throws org.openqa.selenium.ElementNotVisibleException exception**
    
        }
    

    现在,如果你看一下上面的3种方法,似乎没有显示元素,未选中但是启用了IS。因此,当它没有显示时,硒无法识别它。让它可见的java脚本也没有拯救。

    有谁可以帮我解决这个问题。它今天吃了一整天?

1 个答案:

答案 0 :(得分:0)

在您的上一个示例中,我认为您使用'style.visibility'标记时有正确的想法。我建议尝试的另一件事是使用“ExpectedConditions.visibilityOfElementLocatedBy”方法。通常我使用“presenceOfElementLocatedBy”,但如果你在谈论css visibility属性,我认为使用“visibilityOfElementLocatedBy”是要走的路。我认为你可能会发生的事情是你需要等待你试图获得的元素对象的条件,“ExpectedCondtions”方法应该给你你需要的东西。我看到你已经尝试过一些东西,但是你没有列出使用等待条件。没有保证,但你应该尝试一下:

WebDriverWait wait = new WebDriverWait(driver, 60); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(
         By.xpath(".//whatever")))