java 可见性元素定位与可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42038485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:18:51  来源:igfitidea点击:

visibilityOfElementLocated Vs visibilityOf

javaseleniumselenium-webdriverwebdriver

提问by J_Coder

When i tried to run below code, visibilityOfElementLocatedworks perfectly fine and webdriver waits for the element with given time.

当我尝试在下面的代码中运行时,visibilityOfElementLocated工作得很好,并且 webdriver 在给定的时间内等待元素。

dr.get("http://www.seleniumframework.com/Practiceform/");
WebDriverWait wait=new WebDriverWait(dr,30);
WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Element5")));

but the same way if i use visibilityOf, it gives me

但同样的方式,如果我使用visibilityOf,它给了我

NoSuchElementException

无此类元素异常

WebElement we = wait.until(ExpectedConditions.visibilityOf(dr.findElement(By.linkText("Element3"))));

Can you explain me why i am getting this exception?

你能解释一下为什么我会收到这个异常吗?

回答by Saurabh Gaur

but the same way if i use "visibilityOf",it gives me NoSuchElementException

但同样的,如果我使用“visibilityOf”,它会给我 NoSuchElementException

Actually, you are getting Exceptionby this line of code dr.findElement(By.linkText("Element3")), in your provided code this line will execute first and if element will be find then ExpectedConditions.visibilityOf()callable will execute.

实际上,您是Exception通过这行代码获得的dr.findElement(By.linkText("Element3")),在您提供的代码中,该行将首先执行,如果找到元素,ExpectedConditions.visibilityOf()则将执行 callable。

FYI, WebDriver.findElement()either throws exception or returns a WebElement.

仅供参考,WebDriver.findElement()要么抛出异常,要么返回WebElement.

visibilityOfElementLocatedVs visibilityOf:-

visibilityOfElementLocated对比 visibilityOf:-

  • visibilityOfElementLocatedis used for checking that an element is present on the DOM of a page and visible. Means it uses Byobject instead of WebElementobject with callable function to find that element first then check that element is visible or not.

  • visibilityOfis used for checking that an element, known to be present on the DOM of a page, is visible. Means you have already found that element and just check only for that visibility.

  • visibilityOfElementLocated用于检查元素是否存在于页面的 DOM上并且是可见的。意味着它首先使用By对象而不是WebElement具有可调用函数的对象来查找该元素,然后检查该元素是否可见。

  • visibilityOf用于检查已知存在于页面 DOM 上的元素是否可见。意味着您已经找到了该元素,只需检查该可见性即可。

回答by Randomguy

According to this:

根据这个

visibilityOf: Does not check for presenceof the elementas the error explains it.

visibilityOfElementLocated: Checks to see if the element is presentand also visible. To check visibility, it makes sure that the element has a height and width greater than 0.

可见性:不检查元素的存在,因为错误解释了它。

visibilityOfElementLocated:检查元素是否存在并且是否可见。为了检查可见性,它确保元素的高度和宽度大于 0。