简体   繁体   English

如何在Selenium WebDriver中识别隐藏的文件元素

[英]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. 我正在尝试自动执行文件上传功能,但Webdriver无法识别文件对象。 Here is the thing: 这是东西:

  1. The file object is in a modalbox (xpath is of the modal box is //*[@id='modalBoxBody']/div[1]). 文件对象在模式框中(模式框的xpath是// * [@@ == modalBoxBody'] / div [1])。 The type and name of the file object are file and url respectively. 文件对象的类型和名称分别为file和url。
  2. When i see the html content, there are two objects with the same attributes. 当我看到html内容时,有两个具有相同属性的对象。 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. 我已经尝试了stackoverflow中提供的所有可能的解决方案(尽我所能搜索),但是没有任何效果。 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. 现在,如果您查看上面的3种方法,则似乎未显示该元素,未选中该元素,但已启用IS。 So when it is not displayed, selenium cannot identify it. 因此,当硒不显示时,硒无法识别它。 The java script to make it visible also came to no rescue. 使它可见的Java脚本也无法挽救。

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. 在您的最后一个示例中,使用'style.visibility'标记对您来说似乎是正确的主意。 Another thing that I would recommend trying is using "ExpectedConditions.visibilityOfElementLocatedBy" method. 我建议尝试尝试的另一件事是使用“ ExpectedConditions.visibilityOfElementLocatedBy”方法。 Usually I use "presenceOfElementLocatedBy", but if you are talking about the css visibility property, I think using "visibilityOfElementLocatedBy" is the way to go. 通常,我使用“ presenceOfElementLocatedBy”,但是如果您谈论的是CSS可见性属性,我认为使用“ visibilityOfElementLocatedBy”是可行的方法。 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. 我认为对您来说可能会发生的事情是,您需要等待要保留的元素对象的等待条件,而“ ExpectedCondtions”方法应该为您提供所需的条件。 I see that you have tried a few things but you haven't listed using a Wait condition . 我看到您尝试了一些操作,但没有使用Wait条件列出 No guarantees, but you should try it: 没有保证,但是您应该尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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