如何通过Selenium找到此HTML标签?

时间:2019-01-18 14:43:23

标签: selenium selenium-webdriver xpath css-selectors webdriver

HTML:

<img src="dash_img/dash-img_07.png">

HTML图片:

enter image description here

这里没有获取xpath的属性,我通过链接文本尝试过,但这太失败了。

4 个答案:

答案 0 :(得分:0)

要找到该元素,可以使用以下任一解决方案:

  • CssSelector

    div.Timesheet li.time-bg > a > img[src='dash_img/dash-img_07.png']
    
  • XPath

    //div[@class='Timesheet']//li[@class='time-bg']/a/img[@src='dash_img/dash-img_07.png']
    
  • XPath (基于文本查看时间表):

    //span[contains(., 'View Timesheet')]//preceding::a[1]/img
    

答案 1 :(得分:0)

您可以使用此xpath找到它

WebElement temp = driver.findElement(By.xpath("//img[contains(@src,'<img src="dash_img/dash-img_07.png">')]"));

答案 2 :(得分:0)

"//div[@class='list']//a/img"

答案 3 :(得分:0)

I think the most straightforward way to find it would be to use a CSS selector

img[src='dash_img/dash-ig_07.png']

or XPath

//img[@src='dash_img/dash-ig_07.png']

CSS selectors are faster, better supported, and a simpler syntax to learn but there are times when you have to use XPath, e.g. when you want to find an element by contained text.

But... I would suggest that you find it based on the image label, "View Timesheet",

//span[.='View Timesheet ']/preceding::img[1]