如何基于子图像元素单击父元素

How to Click Parent Element Based on Child Image Element

我试图让 Selenium 单击基于子图像元素的父 Web 元素。这是我在页面源代码中使用的元素的片段:

<a id="_20_ivtm" href="https://foobar:443/web/baz/document-library?fedit_file_entry">
    <img id="aui_3_2_0_1724" class="icon" style="background-image: url('/foobar/images/document_library/_sprite.png'); background-position: 50% -16px; background-repeat: no-repeat; height: 16px; width: 16px;" alt="" src="/foobar/images/spacer.png"/>
    Add Document

我想根据其子图像元素中的 "Add Document" 子文本单击 a 父元素。我正在 Java 编码。关于如何最好地完成此任务有什么建议吗?

该文本与 <img> 标签无关,它与 <a> 标签相关。你可以这样找到:

driver.findElement(By.xpath("//a[contains(., 'Add Document')]"));