点击图片作为按钮?

时间:2017-04-03 06:52:55

标签: selenium-webdriver

<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<img src="/images/ic_highlight_removegray.svg" alt="">
</button>

我试过点击这个弹出的(X)图片,这是链接。我已经用所有可能的选择器完成了这个,包括Xpath,class&amp; cssSelectors但没有结果。请建议这是图像/按钮的特殊情况。

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(35000);
driver.findElement(By.xpath(".//*[@id='share_preview_modal']/div/div[1]/button")).click();

我也尝试过与wait,relative&amp;绝对xpath,.sendKeys(Keys.ENTER); - 失败

    <div id="share_preview_modal" class="modal fade pro_info_member in dashboard-modal" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="user_profile" style="display: block;">
<div class="modal-dialog " role="document">
<div class="modal-header">
<div class="preview-header">
<span class="success-msg white_box_info" style="display:none">Restricted email access to domains </span>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<img src="http://some-url/images/ic_highlight_removegray.svg" alt="">
</button>
</div>
<div class="modal-content white-popup " style="background-image: url("");">
<!-- white popoup -->
</div>
</div>

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此xpath单击“关闭”按钮:

driver.findElement(By.xpath("//div[id='share_preview_modal']//button[@class='close']")).click();

driver.findElement(By.xpath("//div[@id='share_preview_modal']//button[@class='close']")).click();

driver.findElement(By.xpath("//div[*id='share_preview_modal']//button[@class='close']")).click();

如果这有助于您,请告诉我。

答案 1 :(得分:-1)

使用Selenium 3,方法“click”有一些问题。如果此方法不起作用,您可以尝试:

driver.findElement(By.xpath("//div[*id='share_preview_modal']//button[@class='close']")).sendKeys(Keys.RETURN);