【问题标题】:How to assert image present in selenium python web driver?如何断言 selenium python web 驱动程序中存在的图像?
【发布时间】:2014-07-02 12:46:14
【问题描述】:

我想断言图像存在于页面上。 我在 python 中使用 selenium web 驱动程序。 我还希望在图像尚未加载时打印一条消息。 谁能帮我写一个脚本?

这是一段在网站上显示图像的 html 代码: [img src='photo.jpg' class='图像类']

【问题讨论】:

  • xpath 选择器,img[src='photo.jpg']

标签: python image selenium webdriver assert


【解决方案1】:

我对 Python 无能为力,但可以为那些希望在 Java 中做类似事情的人提供一般性建议和 Java 代码。我的方法是

  1. 获取图片的src属性,快速生成HTTP GET 致电并确保您收到200 OK 回复。
  2. 一旦完成,你 可以使用检查 WebElement 的 naturalWidthattribute Javascript。如果它大于 0,则渲染得很好。然而这 在 IE 中的工作方式不同。 IE 有.complete 属性。这将是 如果图像被渲染则为真(参考here

这里是示例 Java 代码,

@Test
   public void test()
   {
      WebDriver driver = new FirefoxDriver();
      String url = "http://www.espncricinfo.com/";
      driver.get(url);
      WebElement img = driver.findElement(By.cssSelector("a[title='ESPN Cricinfo']>img"));
      String src = img.getAttribute("src");
      Client client = Client.create();
      WebResource resource = client.resource(src);
      assertThat("Response code is not 200 OK", resource.get(ClientResponse.class).getStatus(), equalTo(200));
      assertThat("Image is not rendered correctly", isImageVisible(driver, img),equalTo(true));
      driver.quit();
   }
   public boolean isImageVisible(WebDriver driver, WebElement image)
   {
      Boolean result = null;
      if (driver instanceof InternetExplorerDriver || ((RemoteWebDriver) driver).getCapabilities().getBrowserName().equals("internet explorer"))
      {
         result = (Boolean) ((JavascriptExecutor) driver).executeScript("return arguments[0].complete;", image);
      }
      else
      { //other browser types use diff method to check
         result = (Boolean) ((JavascriptExecutor) driver).executeScript("return (typeof arguments[0].naturalWidth!=\"undefined\" && arguments[0].naturalWidth>0);", image);
      }
      return result.booleanValue();
   }
}

【讨论】:

  • 问题被标记为“python”。
  • 我知道。我提到我无法在 python 中提供帮助,但给出了可以在 python 中实现的一般建议。提供 Java 代码以防万一有人不小心来到这里并需要 Java 解决方案。只是想帮忙
  • 当然,我明白了。这也是我在这里的原因。我也看到了关于我的答案的要点,但我把它留到了 OP 说它是否有帮助,或者 OP 是否需要更多帮助。
【解决方案2】:

可能有更好的方法来做到这一点,但我决定检索所有图像标签。

all_images = self.driver.execute_script("return document.getElementsByTagName('img')")
for image in range(0, len(all_images)):
    print self.driver.execute_script("return document.getElementsByTagName('img')[{}].src".format(image))

【讨论】:

    【解决方案3】:

    省略了驱动程序设置,但是一旦您添加了驱动程序设置,这应该可以工作。部分归功于 Nilesh,因为他用他的 Java 示例为我指明了正确的方向。

    import requests
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    def setUp(self):
        self.verificationErrors = []
    
    
    def test_images_for_200_response(self):
        driver.get('http:example.com')
        example_images = driver.find_elements(By.TAG_NAME, 'img')
    
        for image in example_images:
            current_link = image.get_attribute("src")
            r = requests.get(current_link)
            try: self.assertEqual(r.status_code, 200)
            except AssertionError, e: self.verificationErrors.append(current_link + ' delivered response code of ' + r.status_code)
    
    def tearDown(self):
        driver.quit()
        self.assertEqual([], self.verificationErrors)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2017-03-03
      相关资源
      最近更新 更多