我有以下html页面源代码,我尝试通过xpath / cssselector单击图像。没有人可以工作,你能帮我找到我的代码问题吗?我使用的是IE9。
<pretable border="0" cellpadding="0" cellspacing="0" width="700">
<tr>
<td rowspan="2" width="120">
<a href="#" onclick="oCMenu.m['top1'].b.moveIt(8,60); oCMenu.showsub('top1'); "
onclick="return false" class="FontNormal">
<img border="0" src="images/shim.gif" width="112" height="73"></a></td>
</tr>
</pretable>
我的代码是:
ieDriver.findElement(By.xpath("//html/table/tr[1]/td/a[@class='FontNormal']/img[@src='images
/shim.gif']")).click();
ieDriver.findElement(By.cssselector("class='FontNormal'")).click();
答案 0 :(得分:2)
您正在尝试提供完整的xpath,但它与您提供的html不匹配。
&#39;表&#39;与'pretable&#39;不同,你也不需要提供完整的路径。相反,只需尝试使用xpath:
XPath("//a[@class='FontNormal']")
在这里,xpath将搜索任何链接与类&#39; FontNormal&#39;连接。如果只有一个,这将选择你的元素。如果还有更多,您可能需要更具体。
答案 1 :(得分:2)
使用可用的DOM结构,我们可以随时使用CSS Selector。
CSS选择器
css=a[href='images/shim.gif']
css=a[href*='shim.gif']
然后执行
driver.findElement(By.cssSelector("a[href='images/shim.gif']")).click();
OR
driver.findElement(By.cssSelector("a[href*='shim.gif']")).click();
答案 2 :(得分:1)
首先编写正确的xpath以标识要执行单击操作的webelement
driver.findElement(By.Xpath("//img[@ src='images/shim.gif']")).click();
答案 3 :(得分:0)
将Firebug与Firepath一起用于Mozilla Firefox。这将自动为您生成xpath。
Webelement element = driver.findElement(By.xpath("*xpath here*");
element.click();
答案 4 :(得分:0)
element(By.xpath('//html/table/tr[1]/td/a[@class='FontNormal']/img[@src='images
/shim.gif']')).click();
答案 5 :(得分:0)
使用xpath单击图像链接。
driver.findElement(By.xpath(“ // img [包含(@src,'/ static / images / image_name.png')]”)))。click();
您可以根据图像来源更改图像路径。