简体   繁体   English

硒:无法单击类和图像

[英]Selenium: unable to click on class and image

I'm using Selenium web driver and trying to click on search image and its unable to click on this. 我正在使用Selenium Web驱动程序,并尝试单击搜索图像,但无法单击它。 I'm not sure what is going wrong. 我不确定发生了什么问题。 I have tried 我努力了

xpath option xpath选项

driver.findElement(By.xpath("//form/div/div/div/div/div[2]/table/tbody/tr/td[2][@class='icon-button combo-button']")).click();

Also tried for image 还尝试了图像

driver.findElement(By.xpath("//form/div/div/div/div/div[2]/table/tbody/tr/td[2]/img[contains(@src,'iconActionSearch.png')]")).click();

I have tried exactly the same from Selenium IDE 我已经从Selenium IDE尝试了完全相同的方法

driver.findElement(By.xpath("//div[@id='divToolbar']/div[2]/table/tbody/tr/td[2]/img")).click();

this is where above code is suppose to act 这就是上面的代码应该起作用的地方

   <td class="icon-button combo-button" title="Search">
    <img src="../common/images/iconActionSearch.png">

this is html snippet i have 这是我有的html片段

<!DOCTYPE html>
<html>
<head>
<body class="navigator" onbeforeunload="closeAllChildWindows()" onload="initializeSlideIns()">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<div class="mmenu" style="display: none;">
<form name="navigatorForm">
<div id="pageHeadDiv">
<div class="toolbars">
<div class="toolbar-container">
<div id="divToolbar" class="toolbar-frame">
<div class="toolbar">
<div class="toolbar functions">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="icon-button" nowrap="" itemid="emx567127522162.935" title="Home">
<td class="icon-button combo-button" title="Search">
<img src="../common/images/iconActionSearch.png">
</td>
<td class="menu-arrow" title="Search">
<td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="breadcrumbs">
<script src="./scripts/emxBreadcrumbs.js" type="text/javascript">
<script src="./scripts/emxQuery.js" type="text/javascript">
<script type="text/javascript">
<div id="pageContentDiv" style="top: 62px;">
<div id="layerOverlay" style="display: none;"></div>
<div id="panelSlideIn" class="dialog viewer" style="top: 62px; display: none;">
<div id="windowshadeMask" style="z-index: 50; display: none;">
</form>
<div id="checkoutdiv">
</body>
</html>

try using css selectors alternative: 尝试使用CSS选择器替代方法:

String cssSelector="td[class='icon-button combo-button']>img"
driver.findElement(By.cssSelector(cssSelector)).click();

a piece of advice: use firebug, firefox extension to verify you located element properly 一条建议:使用firebug,firefox扩展程序来验证您正确定位了元素 在此处输入图片说明

Just as a different solution to eugene (which, by the way, is a better way of doing it since the CSS selector will be faster), if you must use XPath: 正如必须使用XPath的另一种方法来解决eugene(顺便说一句,这样做是一种更好的方法,因为CSS选择器会更快):

//td[@class='icon-button combo-button']/img

Your original XPath solutions will never work, they are very confined to specific positions. 您原始的XPath解决方案将永远无法使用,它们只能在特定位置使用。 You put in an extra div somewhere and your entire XPath will fall over. 您在某个地方放了一个div,整个XPath就会崩溃。 So go for something that's a little less brittle. 因此,选择一些不那么脆弱的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM