验证图像是否已通过Selenium正确加载并在网站上可见。

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

Validate Images are loaded properly and visible on website using Selenium

问题

验证场景如下:
在进行网站检查时,使用URL响应,我们会得到200 OK作为响应。有时候我们会观察到,由于某些CSS文件或JS文件加载错误,我们无法看到网站上的图像加载情况。

我需要使用Selenium和Java在给定的场景中进行验证。

英文:

Validation Scenario as follows :
During website check using URL response we get 200 OK as response. There are instances observed when due to some CSS file or JS file loading error we are not able to see images on website getting loaded.

I need to do validation in given scenario using Selenium and Java.

答案1

得分: 1

以下是翻译好的部分:

List<String> imageAlts = {"imageAlt1", "imageAlt2"};
List<WebElement> images = driver.findElements(By.tagName("img"));
for (WebElement image: images) {
    String imageAlt = image.getAttribute("alt");
    if (imageAlts.contains(imageAlt)) {
        int x = image.getLocation().getX();
        int y = image.getLocation().getY();
        int w = image.getRect().getWidth();
        int h = image.getRect().getHeight();
        if (x > 0 && y > 0 && w > 0 && h > 0) {
            System.out.println("image with alt " + imageAlt + " is visible");
        }
        else {
            System.out.println("image with alt " + imageAlt + " is NOT visible");
        }
    }
    else {
        System.out.println("image with alt " + imageAlt + " NOT found");
    }
}
英文:

What about this:

List&lt;String&gt; imageAlts = {&quot;imageAlt1&quot;, &quot;imageAlt2&quot;};
List&lt;WebElement&gt; images = driver.findElements(By.tagName(&quot;img&quot;);
for (WebElement image: images) {
	String imageAlt = image.getAttribute(&quot;alt&quot;);
	if (imageAlts.contains(imageAlt)) {
		int x = image.getLocation().getX();
		int y = image.getLocation().getY();
		int w = image.getRect().getWidth();
		int h = image.getRect().getHeight();
		if (x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; w &gt; 0 &amp;&amp; h &gt; 0) {
			System.out.println(&quot;image with alt &quot; + imageAlt + &quot; is visible&quot;);
		}
		else {
			System.out.println(&quot;image with alt &quot; + imageAlt + &quot; is NOT visible&quot;);
		}
	}
	else {
		System.out.println(&quot;image with alt &quot; + imageAlt + &quot; NOT found&quot;);
	}
}

huangapple
  • 本文由 发表于 2020年10月16日 01:32:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376893.html
匿名

发表评论

匿名网友

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

确定