简体   繁体   中英

How to identify a hidden file element in selenium webdriver

Team,

I am trying to automate a file upload functionality but webdriver doesn't recognize the file object. Here is the thing:

  1. The file object is in a modalbox (xpath is of the modal box is //*[@id='modalBoxBody']/div[1]). The type and name of the file object are file and url respectively.
  2. When i see the html content, there are two objects with the same attributes. One of them is visible and another is invisible. But the hierarchy they belong to are different. So I am using the hierarchy where the element is visible.

Following is my code. I have tried all possible solutions provided in the stackoverflow (as much as I could search), but nothing worked. Commented out sections mean that they too are tried and failed.

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**

    }

Now, if you look at the 3 methods above, it seems that the element is NOT displayed, NOT selected but IS enabled. So when it is not displayed, selenium cannot identify it. The java script to make it visible also came to no rescue.

Could anyone please help me solve this. It ate my entire day today?

In your last example, it looks to me like you have the right idea with using the 'style.visibility' tag. Another thing that I would recommend trying is using "ExpectedConditions.visibilityOfElementLocatedBy" method. Usually I use "presenceOfElementLocatedBy", but if you are talking about the css visibility property, I think using "visibilityOfElementLocatedBy" is the way to go. I think what might be happening for you is that you need the wait condition on the element object you are trying to get a hold of and the "ExpectedCondtions" method should give you what you need. I see that you have tried a few things but you haven't listed using a Wait condition . No guarantees, but you should try it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM