【问题标题】:Unable to identify a webelement using xpath无法使用 xpath 识别 Web 元素
【发布时间】:2015-04-21 19:20:02
【问题描述】:

我必须单击应用程序中的关闭按钮。因此,我尝试使用 xpath 来识别元素,但最终发现元素不可见异常。所以,我只使用了Thread.sleep(5000),它确实抛出了No Such Element exception,然后

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//img[@src='https://healthhomestg.gsihealth.com:6161/dashboard/dashboardapp/sc/skins/EnterpriseBlue/images/headerIcons/close.png']"))); 

但我最终还是得到了No such element exception。然后 div 标签中的 id 和 img 标签中的 name 会在每次页面加载时动态更改。请帮我识别关闭按钮并点击它

我使用的xpath:

"//img[@src='https://healthhomestg.gsihealth.com:6161/dashboard/dashboardapp/sc/skins/EnterpriseBlue/images/headerIcons/close.png']"

关闭按钮的代码 sn-p 如下所示

<div id="isc_9L" class="imgButton" role="button" onscroll="return isc_ManageCareTeamWindow_0_closeButton.$lh()" style="position: absolute; left: 1057px; top: 2px; width: 15px; height: 15px; z-index: 202052; overflow: hidden; box-sizing: border-box; cursor: pointer; display: inline-block;" eventproxy="isc_ManageCareTeamWindow_0_closeButton">

<img width="15" height="15" border="0" align="TEXTTOP" draggable="true" suppress="TRUE" name="isc_9Lmain" src="https://healthhomestg.gsihealth.com:6161/dashboard/dashboardapp/sc/skins/EnterpriseBlue/images/headerIcons/close.png">

</div>

【问题讨论】:

    标签: selenium xpath selenium-webdriver


    【解决方案1】:
    driver.findElement(By.cssSelector("img[src*='close.png']")); 
    

    所以,这里我们通过cssSelector来识别元素IMG,它里面有src属性,可以包含“close.png”。

    【讨论】:

    • 谢谢,斯坦杰。我尝试了您提供的代码,但没有发生单击操作。需要强调的是,被测应用程序是使用 GWT 开发的,我猜 user-extensions.js 和 user-extensions-ide.js 必须使用 IDE 或 Selenium RC 进行配置,但我应该在 webdriver 中配置它们(selenium )
    【解决方案2】:

    试试这个:

     //img[contains(@src,'headerIcons/close.png')]
    

    【讨论】:

    • 谢谢,阿迪亚。我在 xpath 中尝试了它,但没有执行单击操作,尽管我使用了 Thread.sleep()、隐式和 WebDriverWait,但最终出现以下错误。该应用程序是使用 GWT 开发的,我猜 user-extensions.js 和 user-extensions-ide.js 必须使用 IDE 或 Selenium RC 进行配置,但我不知道如何使用线程“主”组织中的 webdriver 异常来做到这一点。 openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2020-06-20
    • 2018-03-18
    • 1970-01-01
    • 2020-08-16
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多