1

background-image:url('.....')コンテナーのプロパティにアクセスしdivて、画像の URL を取得しようとしています。

問題は、コンテナー div が最初は非表示で、画像の URL が動的に追加され、コンテナーが表示されることです。

それを使用して要素にアクセスしようとすると、photoContainer.getCssValue("background-image");me が返されますnone

HTMLコードは次のとおりです。

<div id="photo_container" style="background-image: url("res/images/840x460/287/28702560.jpg");"></div>

この div の CSS は次のとおりです。

background-image: url("res/images/840x460/287/28702560.jpg");
display: none;

ただし、以下のコードを使用すると、URL 文字列を取得できます。

       WebDriverWait wait = new WebDriverWait(driver, 20);
       WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("photo_holding")));
       // Now div is visible so get the attribute
       WebElement photoContainer = driver.findElement(By.id("photo_holding"));
       String imgUrl = photoContainer.getCssValue("background-image");
       System.out.println(imgUrl);

   driver.quit();

画像は、一定の時間間隔を使用してスライドショーとして読み込まれます。私の目的は、動的に読み込まれた画像の URL を取得することです。

編集 :

以下のコードは、スライド ショーから画像の URL を取得するために使用されます。

 for(int i=0;i<=slideCount;i++){
       WebDriverWait wait = new WebDriverWait(driver, 20);
       WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("photo_holding")));
       // Now div is visible so get the attribute
       WebElement photoContainer = driver.findElement(By.id("photo_holding"));
       String imgUrl = photoContainer.getCssValue("background-image");
       System.out.println(imgUrl);
       Thread.sleep(2000);
   }

画像コンテナ「photo_holding」は、以下のコードを使用して動的に追加されます

$('<div id="photo_holding"></div>').insertAfter("#photo_container");$("#photo_holding").css("background-image","url("+slideshow_photos[o]+")");
 $("#photo_container").fadeOut(2000,function() {$(this).remove();$("#photo_holding").attr("id","photo_container");
4

2 に答える 2

0

ImageURL は に保存されslideshow_photos[]ますね。それを読めば、必要なものを手に入れることができるかもしれません。

Reading JavaScript variables using Selenium WebDriver 3rd answer によると、とにかくスライドの数を知る必要がある場合は、それらをループで取得できる可能性がありますString result = (String)js.executeScript("return slideshow_photos["+i+"]");

これがお役に立てば幸いです。

于 2014-04-16T10:02:44.853 に答える