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
visibilityOfElementLocated Vs visibilityOf
提问by J_Coder
When i tried to run below code, visibilityOfElementLocated
works 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 Exception
by 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
.
visibilityOfElementLocated
Vs visibilityOf
:-
visibilityOfElementLocated
对比 visibilityOf
:-
visibilityOfElementLocated
is used for checking that an element is present on the DOM of a page and visible. Means it usesBy
object instead ofWebElement
object with callable function to find that element first then check that element is visible or not.visibilityOf
is 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。