英文:
Why isDisplayed() method is returning false even if the WebElement is present?
问题
这是我的代码:
@FindBy(css = "span.et_pb_image_wrap img[title='globeathome ']")
WebElement header1;
我为此创建了一个方法,因为我正在使用POM设计模式:
public boolean isImgDisplayed() {
return header1.isDisplayed();
}
这是HTML标签:
<span class="et_pb_image_wrap ">
<img src="https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome.png" alt="globeathome stratpoint" title="globeathome" srcset="https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome.png 2855w, https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome-1280x1037.png 1280w, https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome-980x794.png 980w, https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome-480x389.png 480w" sizes="(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) and (max-width: 1280px) 1280px, (min-width: 1281px) 2855px, 100vw">
</span>
我的问题是,即使WebElement存在,isDisplayed() 方法返回 false。
英文:
Here's my code:
@FindBy(css = "span.et_pb_image_wrap img[title='globeathome ']")
WebElement header1
I created a method for this since I am using a POM design pattern:
public boolean isImgDisplayed()
{
return header1.isDisplayed();
}
here's the HTML tag :
<span class="et_pb_image_wrap ">
<img src="https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome.png" alt="globeathome stratpoint" title="globeathome" srcset="https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome.png 2855w, https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome-1280x1037.png 1280w, https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome-980x794.png 980w, https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome-480x389.png 480w" sizes="(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) and (max-width: 1280px) 1280px, (min-width: 1281px) 2855px, 100vw">
</span>
My problem is the isDisplayed() is returning false even if the WebElement is present.
答案1
得分: 2
检查文档 -> https://developers.perfectomobile.com/display/TT/Difference+between+Selenium+visibility+methods
isDisplayed()
方法:
该方法用于确定元素是否显示在屏幕上,即其宽度和高度是否大于零,是否被CSS隐藏等。如果元素存在于页面上,但具有style="display:none;",则isDisplayed()将返回false。如果元素显示,则返回true;如果不显示,则返回false。该方法的优点是避免解析元素的style属性。isDisplayed用于元素在DOM中存在的情况下,您需要检查它是否在UI中显示。它从不用于检查元素是否存在于DOM中。
如@Razvan所说,图片可能未加载。
英文:
check doc -> https://developers.perfectomobile.com/display/TT/Difference+between+Selenium+visibility+methods
isDisplayed()
:
This method determines if an element is displayed on the screen or not i.e. whether its width and height are greater than zero, it isn't hidden by CSS, etc. If the element is present on the page, but has style="display:none;" then isDisplayed() will return false.. It returns true if the element is displayed and false if it is not. Advantage of this method is that it avoids parsing an elements style attribute. isDisplayed is used in cases where element is present in DOM and you need to check whether it is displayed or not in the UI. It is never used to check whether an element is present in the DOM.
as @Razvan said image may not be loaded
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论