2013-06-04 63 views
2

我對硒這個奇妙的工具很陌生,我試圖在我的web應用程序(html/JS)中做一些示例測試。 我設法用命令driver.findElement(By.id("elementId")); 選擇一些(大多數)元素與他們的ID,但我找不到一些沒有id標籤的元素。 我嘗試了這些下面的行沒有結果,如我有具有在XPath中兩個aboce的selenium findElement by By.id

  • By.cssSelector("//img[@alt='smthg']")
  • By.xpath("//img[@src='path/to/img'")
  • 混合(ALT和src和cssSelector

該元素HTML代碼是 <img src="absolut/path/to/img.png" border="0" onclick="JSfunction(0)" alt="smthg" style="cursor: pointer;">

如果有人可以幫助我,那會很ni ce :) 謝謝,祝你有美好的一天!

回答

4

您可以使用以下

By.cssSelector("img[alt='smthg'][src*='path/to/img']"); 

or 

By.xpath("//img[@alt='smthg' and contains(@src,'path/to/img')]") 
+0

好吧是我不好,我好像輸錯在字符串小寫字符... 無論如何,感謝你的幫助,你的方法現在做的工作:) – Matt

相關問題