我正在尝试单击图像按钮,但是却出现org.openqa.selenium.NoSuchElementException。我什至通过迭代框架来检查元素是否存在于任何iframe中。它不在任何框架中。我想念什么吗?有人可以帮我吗?
下面是我正在尝试的代码:
WebDriverWait wait2 = new WebDriverWait(driver,30);
WebElement image = wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr//table[@title='User Prompt Input']//div[@id='IconImg_vertBar_leftPaneW_icon_3']")));
actions.click(image).build().perform();
我得到的异常:
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 45 seconds waiting for visibility of element located by By.xpath: //tr//table[@title='User Prompt Input']//div[@id='IconImg_vertBar_leftPaneW_icon_3']
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'W1856237', ip: '192.168.43.137', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
at com.ExpTestcases.TestOne.main(TestOne.java:105)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//tr//table[@title='User Prompt Input']//div[@id='IconImg_vertBar_leftPaneW_icon_3']"}
(Session info: chrome=70.0.3538.77)
这是图像按钮的html代码:
<table role="button" aria-label="User Prompt Input" aria-pressed="false" title="User Prompt Input" style="display: block; cursor: pointer;" false"="" margin:0px"="" id="vertBar_leftPaneW_icon_3" class="" cellspacing="0" cellpadding="0" border="0" aria-labelledby="ariaLabelledBy_vertBar_leftPaneW_icon_3" tabindex="0">
<tbody>
<tr valign="middle">
<td id="iconleft_vertBar_leftPaneW_icon_3" class="">
<div style="width:2px;height:35px"></div>
</td>
<td id="iconmid_vertBar_leftPaneW_icon_3" align="center" style="height:35px;" class="">
<div style="overflow:hidden;height:31px;" valign="middle">
<div id="IconImg_vertBar_leftPaneW_icon_3" class="imo" style="width:31px;height:31px;background-image:url('images/main/vertbar_icons.png');background-position:-1px -127px;margin-top:2px;cursor:pointer"></div>
</div>
</td>
<td id="iconright_vertBar_leftPaneW_icon_3" class="">
<div style="width:2px;height:35px"></div>
</td>
</tr>
</tbody>
</table>
Xpath试用版:
// table [@ aria-label =“用户提示输入”]
// tr // table [@ title ='用户提示输入'] // div [@ id ='IconImg_vertBar_leftPaneW_icon_3']
// table [@ aria-label =“用户提示输入”] / @ title
// tr // table [@ aria-label =“用户提示输入”] / @ title
答案 0 :(得分:0)
当您尝试在元素上调用click()
时,您需要使用 visibilityOfElementLocated()
方法而不是使用elementToBeClickable()
方法,并且可以使用以下任一方法以下解决方案:
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("table[id^='vertBar_leftPaneW_icon_'][aria-label='User Prompt Input'] div.imo[id^='IconImg_vertBar_leftPaneW_icon_'][style*='vertbar_icons']"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[starts-with(@id,'vertBar_leftPaneW_icon_') and @aria-label='User Prompt Input']//div[@class='imo' and starts-with(@id,'IconImg_vertBar_leftPaneW_icon_')][contains(@style,'vertbar_icons')]"))).click();