我正在使用Selenium Webdriver,Java& TestNG的。我正在尝试从HTML表中搜索图像,如果找到该图像,我需要单击其他按钮。 例如:
这是html代码
<th scope="col">
Detail
</th>
<th class="sortable" scope="col">
<a href="javascript:__doPostBack('reprot5','test$DateScored')">
Received
</a>
</th>
<th scope="col">
<th scope="col">
Alert
</th>
<th class="report6" scope="col">
Response
</th>
<th scope="col"/>
</tr>
</thead>
<tbody>
<tr class="reprot7">
<td>
test survey
</td>
<td>
<span>
Agent--Equiniti
</span>
</td>
<td>
<span>
28 November 2013 10:47:59
</span>
</td>
<td>
<span/>
</td>
<td>
<span>
<img class="justprintingImg" style="padding-left:10px;" src="../icon_alert.gif"/>
</span>
</td>
答案 0 :(得分:0)
虽然没有经过测试,但我认为以下内容会给你一个相同的想法。
WebElement image = driver.findElement(By.xpath("//img[@src='../icon_alert.gif"));
if(image!=null){
//button click event
}
答案 1 :(得分:0)
尝试此代码
Boolean bool=driver.findElement(By.xpath("//img[@class='justprintingImg')).isDisplayed();
(OR)
Boolean bool=driver.findElement(By.xpath("//img[@src='../icon_alert.gif')).isDisplayed();
if(bool){
Click operation
}
希望这有助于你
-Ajay
答案 2 :(得分:0)
我在这里做了很多假设,但我认为你正在寻找的是使用try / catch块
try
{
//Assuming there is only one row in this table, otherwise you'll need to modify the below to loop through each row
WebElement alertImage = driver.findElement(By.xpath("//img[@class='justprintingImg']"));
//Alert image is present, so do something with it...
alertImage.click();
}
catch (NoSuchElementException nsee)
{
//Alert image is not present, do nothing
}
//continue on
答案 3 :(得分:0)
图片需要一段时间才能加载,如果使用Explicit wait
来定位图像会更好..
boolean is_image_present = false;
try
{
new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img.justprintingImg")));
is_image_present = true;
}catch(Exception e)
{
System.out.println("image not present!!!");
}
if(is_image_present)
{
//code to click on the button
}