【问题标题】:Selenium WebDriver Java. Unable to locate imageSelenium WebDriver Java。找不到图片
【发布时间】:2016-01-19 09:24:48
【问题描述】:

我正在尝试确定以下图像/图标的元素。

注意:其他图标具有相同的 //div[@class='infor-collapsed-icon-img' 所以我认为我需要另一个唯一的 id 来识别下面的确切元素。顺便说一句,ID 是动态的

这是我迄今为止使用 xpath 尝试过的:

1.) //div[@class='infor-collapsed-icon-img' and contains(@title,'Print Manager - Print Manager webpart allows the Lawson workspace user to contextually filter the print files of batch Jobs.')]

2.) //img[@title='Print Manager - Print Manager webpart allows the Lawson workspace user to contextually filter the print files of batch Jobs.']

3.) //img[contains(@title,'Print Manager - Print Manager webpart allows the Lawson workspace user to contextually filter the print files of batch Jobs.')]

对此有什么想法吗?谢谢

【问题讨论】:

    标签: java html selenium xpath selenium-webdriver


    【解决方案1】:

    试试这个 XPath。首先选择div,然后选择其中的img标签。

    "//div[@class='infor-collapsed-icon-img']/img"
    

    编辑1:如果您想获取特定图像,则可以使用标记的id 属性获取它

    "//img[@id='img_WebPartTitlect100_m_g_f26cdbcd_963c_46f4_94b1_c6a4fd7a9aab']"
    

    或按顺序出现的索引。 (我会推荐这个,因为它更干净)

    "(//div[@class='infor-collapsed-icon-img']/img)[1]"
    

    编辑 2: 尝试使用 contains() 部分匹配文本。

    "//div[@class='infor-collapsed-icon-img']/img[contains(@title, 'Print Manager')]"
    

    【讨论】:

    • 嗨,图像上的其他图标在同一个 div 类中。我想我必须寻找元素的确切 ID。更新了上面的线程。
    • id 是动态的。图标可以添加和删除。它出现的指数也可以随机变化。我可以不使用图标的@title 吗?我正在考虑将div classtitle 结合起来......但我已经尝试过了。它也不起作用..
    • 这行得通,伙计。所以 @title "Print Manager" 单独可以工作。我之前所做的是我包含了@title 的所有描述。非常感谢朋友。
    • @user3713453 - 很高兴我能帮上忙。 :)
    【解决方案2】:

    如果你想要第一张图片

    //div[@class='infor-collapsed-icon-img']/img[1]
    

    如果你想要第二张图片

    //div[@class='infor-collapsed-icon-img']/img[2]
    

    希望对你有帮助:)

    【讨论】:

      【解决方案3】:

      你可以通过ID找到元素

      driver.findElement(By.id("imgId"));
      

      ID 是唯一的,因此您将拥有特定的元素。

      对于您的img_WebPartTitlect100...,在src 属性之后查找id 属性。

      编辑:

      你也可以试试

      driver.findElement(By.cssSelector("[title*='Print Manager']"));
      

      这将为您提供具有包含“打印管理器”的标题的元素。

      【讨论】:

      • @user3713453 我使用标题添加了另一个解决方案。
      【解决方案4】:

      试试这个:

      driver.findElement(By.Xpath("//img[@src='your source']"); 
      

      【讨论】:

        【解决方案5】:

        试试这个:

        //table[@class='infor-collapsed-pane']/tr[1]/td//img

        【讨论】:

          猜你喜欢
          • 2016-04-17
          • 2016-04-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-11
          • 2012-08-05
          • 2016-09-07
          • 2020-03-19
          相关资源
          最近更新 更多