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");