为什么 isDisplayed() 方法返回 false,即使 WebElement 存在?

huangapple go评论90阅读模式
英文:

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 = &quot;span.et_pb_image_wrap img[title=&#39;globeathome &#39;]&quot;)
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 :

&lt;span class=&quot;et_pb_image_wrap &quot;&gt;
&lt;img src=&quot;https://3p4expkcmfr6hgud4mqt.stratpoint.com/wp-content/uploads/globeathome.png&quot; alt=&quot;globeathome stratpoint&quot; title=&quot;globeathome&quot; srcset=&quot;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&quot; sizes=&quot;(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&quot;&gt;
&lt;/span&gt;

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

huangapple
  • 本文由 发表于 2020年10月5日 22:20:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64210512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定