【发布时间】:2014-05-31 01:03:14
【问题描述】:
我正在尝试访问容器 div 的 background-image:url('.....') 属性并获取图像 url。
问题是容器div最初是隐藏的,图像url是动态添加的,容器是可见的。
当我尝试使用photoContainer.getCssValue("background-image"); 访问元素时,它返回给我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
编辑:
以下代码用于从幻灯片中获取图片网址:
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");
【问题讨论】:
-
只是建议不要使用
Thread.sleep,driver.get()等待页面完全加载
标签: html css selenium selenium-webdriver imageurl