Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html selenium Webdriver get()之后,isDisplayed()上的所有元素是否都应返回true?_Html_Asynchronous_Selenium_Webdriver_Rendering - Fatal编程技术网

Html selenium Webdriver get()之后,isDisplayed()上的所有元素是否都应返回true?

Html selenium Webdriver get()之后,isDisplayed()上的所有元素是否都应返回true?,html,asynchronous,selenium,webdriver,rendering,Html,Asynchronous,Selenium,Webdriver,Rendering,WebDriver get()和isDisplayed()方法未按预期工作 至于: 这是使用HTTP GET操作完成的,该方法将阻止 直到装载完成 正如在其他类似问题中提到的,get方法应该等待页面加载 但在运行get()之后,RenderedWebElement中的isDisplayed()方法在某些元素上并不总是返回true 可能的原因是什么 我想详细说明一下在webdrivers的上下文中加载和显示的区别。isDisplayed()在我看来不是很好的方法。从wait中获得一些想法:exlp

WebDriver get()和isDisplayed()方法未按预期工作

至于:

这是使用HTTP GET操作完成的,该方法将阻止 直到装载完成

正如在其他类似问题中提到的,get方法应该等待页面加载

但在运行get()之后,RenderedWebElement中的isDisplayed()方法在某些元素上并不总是返回true

可能的原因是什么


我想详细说明一下在webdrivers的上下文中加载和显示的区别。

isDisplayed()在我看来不是很好的方法。从wait中获得一些想法:exlplicit和隐式等待机制:

Explicit wait
WebDriverWait.until(condition-that-finds-the-element)

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


Explicit Waits:

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.id("myDynamicElement"));
    }});

Implicit Waits:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

The example what you have given both do exact same thing.. 
In Explicit wait, WebDriver evaluates the condition every 500 
milliseconds by default ..if it is true, it comes out of loop 

Where as in ImplicitWait WebDriver polls the DOM every 500 
milliseconds to see if element is present.. 

Difference is 
1. Obvious - Implicit wait time is applied to all elements in your 
script but Explicit only for particular element 
2. In Explicit you can configure, how frequently (instead of 500 
millisecond) you want to check condition. 
3. In Explicit you can also configure to ignore other exceptions than 
"NoSuchElement" till timeout.. 
显式等待
WebDriverWait.until(查找元素的条件)
隐式等待
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
显式等待:
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://somedomain/url_that_delays_loading");
WebElement myDynamicElement=(新的WebDriverWait(驱动程序,10))
.直到(新的预期条件){
@凌驾
公共WebElement应用(WebDriver d){
返回d.findElement(By.id(“myDynamicElement”);
}});
隐式等待:
WebDriver=newfirefoxdriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
驱动程序。获取(“http://somedomain/url_that_delays_loading");
WebElement myDynamicElement=driver.findElement(By.id(“myDynamicElement”);
你给出的例子两者的作用完全相同。。
在显式等待中,WebDriver每500次评估一次条件
默认情况下为毫秒。如果为真,则从循环中得出
其中,隐式等待WebDriver每500次轮询一次DOM
毫秒以查看元素是否存在。。
区别在于
1.显而易见-隐式等待时间应用于
脚本,但仅对特定元素显式
2.在Explicit中,您可以配置频率(而不是500
毫秒)您要检查条件。
3.在显式中,您还可以配置为忽略除
“无接触元素”直到超时。。
你可以得到更多的信息

此外,我还使用fluent等待机制来等待在页面上呈现的元素。实际上,您可以将css选择器或xpath传递给函数,只需获取web元素即可

public WebElement fluentWait(final By locator){
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

        WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                        return driver.findElement(locator);
                }
                }
);
                           return  foo;              }     ;
publicwebelement fluentWait(最终由定位器确定){
等待等待=新建FluentWait(驱动程序)
.带超时(30,时间单位。秒)
.轮询间隔(5,时间单位。秒)
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(
新函数(){
公共WebElement应用(WebDriver){
返回驱动程序findElement(定位器);
}
}
);
返回foo;};
流畅的等待

希望这一点现在清楚了)

isDisplayed()在我看来不是很好的方法。从wait中获得一些想法:exlplicit和隐式等待机制:

Explicit wait
WebDriverWait.until(condition-that-finds-the-element)

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


Explicit Waits:

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.id("myDynamicElement"));
    }});

Implicit Waits:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

The example what you have given both do exact same thing.. 
In Explicit wait, WebDriver evaluates the condition every 500 
milliseconds by default ..if it is true, it comes out of loop 

Where as in ImplicitWait WebDriver polls the DOM every 500 
milliseconds to see if element is present.. 

Difference is 
1. Obvious - Implicit wait time is applied to all elements in your 
script but Explicit only for particular element 
2. In Explicit you can configure, how frequently (instead of 500 
millisecond) you want to check condition. 
3. In Explicit you can also configure to ignore other exceptions than 
"NoSuchElement" till timeout.. 
显式等待
WebDriverWait.until(查找元素的条件)
隐式等待
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
显式等待:
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://somedomain/url_that_delays_loading");
WebElement myDynamicElement=(新的WebDriverWait(驱动程序,10))
.直到(新的预期条件){
@凌驾
公共WebElement应用(WebDriver d){
返回d.findElement(By.id(“myDynamicElement”);
}});
隐式等待:
WebDriver=newfirefoxdriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
驱动程序。获取(“http://somedomain/url_that_delays_loading");
WebElement myDynamicElement=driver.findElement(By.id(“myDynamicElement”);
你给出的例子两者的作用完全相同。。
在显式等待中,WebDriver每500次评估一次条件
默认情况下为毫秒。如果为真,则从循环中得出
其中,隐式等待WebDriver每500次轮询一次DOM
毫秒以查看元素是否存在。。
区别在于
1.显而易见-隐式等待时间应用于
脚本,但仅对特定元素显式
2.在Explicit中,您可以配置频率(而不是500
毫秒)您要检查条件。
3.在显式中,您还可以配置为忽略除
“无接触元素”直到超时。。
你可以得到更多的信息

此外,我还使用fluent等待机制来等待在页面上呈现的元素。实际上,您可以将css选择器或xpath传递给函数,只需获取web元素即可

public WebElement fluentWait(final By locator){
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

        WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                        return driver.findElement(locator);
                }
                }
);
                           return  foo;              }     ;
publicwebelement fluentWait(最终由定位器确定){
等待等待=新建FluentWait(驱动程序)
.带超时(30,时间单位。秒)
.轮询间隔(5,时间单位。秒)
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(
新函数(){
公共WebElement应用(WebDriver){
返回驱动程序findElement(定位器);
}
}
);
返回foo;};
流畅的等待


希望这一点现在清楚了)

在最新的UI框架/APi中,您可以在页面上隐藏元素

例如,考虑一个具有5个元素的页面。加载页面时,页面上将只显示3个元素,其他2个元素将被隐藏,并且在执行某些操作时,将显示其他2个元素

您可以在以下链接的演示部分查看示例:

显示元素链接:

隐藏元素链接:

使用webDriver get()方法时,webDriver将等待页面加载,即等待页面的所有html内容加载到浏览器上。这并不意味着所有元素都是可见的

使用isDisplayed()时,webdriver会检查所述元素是否显示在屏幕上