Click on an image which changes dynamically

时间:2016-04-21 22:37:10

标签: java selenium

I have a set of images whose properties change every time the web app is executed. I need to click one of them in particular, but I can't identify it uniquely.

Here is my current code:

List <WebElement> ListOfimages =driver.findElements(By.className("thumb"));

for (WebElement image : ListOfimages) {
    System.out.println("image: " + image.getAttribute("src"));
}

I'm getting all of them but in the console I only see this:

image :
image :
image :
image :
image :
image :
image :
image :
image :

Here is the HTML code:

<label>Selecciona tu imagen de seguridad.</label>
<div class="aligncenter-img-carosal">
<div id="thumbs" class="margin0">
<div class="item active">
<ul class="thumbs noscript mrgn-lft-minus20">
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle0"     alt="/WebTeal/resources/common/images/security_images/e27776110f72158c8f1f5e36d0  ef3ceaa225bc76fac5b55d4ab34383f00a9e6b"   onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this).parent().parent().addClass('selected')" src="/WebTeal/resources/common/images/security_images/e27776110f72158c8f1f5e36d0ef3ceaa225bc76fac5b55d4ab34383f00a9e6b">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle1"  alt="/WebTeal/resources/common/images/security_images/0f5fa5789f8ce7992a19b9769b 2928f74814eee52277933c68ba60f6653a253d"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/0f5fa5789f8ce7992a19b9769b 2928f74814eee52277933c68ba60f6653a253d">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle2"  alt="/WebTeal/resources/common/images/security_images/5a26b22171215c52666b0b9daf 8d14d49320d6be23ec9dca0d78dd5885a43299"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/5a26b22171215c52666b0b9daf 8d14d49320d6be23ec9dca0d78dd5885a43299">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle3"    alt="/WebTeal/resources/common/images/security_images/98a43db45f0ae431c4b5248697 4ee55952c9f8e18524b42758f69cc1a806a940"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/98a43db45f0ae431c4b5248697 4ee55952c9f8e18524b42758f69cc1a806a940">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle4"  alt="/WebTeal/resources/common/images/security_images/856113c50b3bfd77ee7e37ad02 c1b59099819306bf47c15e34d1ed86c7c2baed"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/856113c50b3bfd77ee7e37ad02 c1b59099819306bf47c15e34d1ed86c7c2baed">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle5"  alt="/WebTeal/resources/common/images/security_images/5666c6ea2bd28dd0dfc2b729ad 4463468e329119d451b2fe527c977be50f4f3d"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/5666c6ea2bd28dd0dfc2b729ad 4463468e329119d451b2fe527c977be50f4f3d">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle6"    alt="/WebTeal/resources/common/images/security_images/5c72f4c79762696f172fb57f84 04989913bd1eccb51b40d96b6b253e4709c7f0"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/5c72f4c79762696f172fb57f84 04989913bd1eccb51b40d96b6b253e4709c7f0">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle7"  alt="/WebTeal/resources/common/images/security_images/9ff2c1606e3f7c0e826ea450e3 f370edcc4056238536e5150de1c1c720530787"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this). parent().parent().addClass('selected')"  src="/WebTeal/resources/common/images/security_images/9ff2c1606e3f7c0e826ea450e3 f370edcc4056238536e5150de1c1c720530787">
</a>
</li>
<li>
<a class="thumb" rel="history" href="#">
<img id="secImgTitle8"     alt="/WebTeal/resources/common/images/security_images/2ddf13da509afa241f331c2f9231b47d2886c5fcf0299608ccd391b30b0772b3" onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this).parent().parent().addClass('selected')" src="/WebTeal/resources/common/images/security_images/2ddf13da509afa241f331c2f9231b47d2886c5fcf0299608ccd391b30b0772b3">
</a> 
</li>
<li style="margin-left: 34.1% !important">
<a class="thumb" rel="history" href="#">
<img id="secImgTitle9"   alt="/WebTeal/resources/common/images/security_images/fccead8b650707de222a5a2f71 9cf0020ba881331b79e01704b7a3d496d0c9d1"  onclick="$('li.selected').removeClass('selected');selectThisImage(this);$(this).parent().parent().addClass('selected')" src="/WebTeal/resources/common/images/security_images/fccead8b650707de222a5a2f719cf0020ba881331b79e01704b7a3d496d0c9d1">

1 个答案:

答案 0 :(得分:1)

“拇指”类位于<a>标签上,而不在图像上。将findElements()来电更改为By.cssSelector(".thumb > img")