有没有办法在没有唯一 ID 的 Selenium 网页上选择项目? [英] Is there a way to select an item on a webpage with Selenium with no unique ID?

查看:21
本文介绍了有没有办法在没有唯一 ID 的 Selenium 网页上选择项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是打开一个网页,然后单击特定应用程序的应用程序按钮,例如 Anaplan.过去,我使用通过 CSS 选择器获取元素以及类和 ID 的组合,

Anaplan App 按钮的 HTML 输出:

<section class="chiclet--main";data-se=app-card-main"><o-tooltip content="Anaplan"位置=底部"类=水合的"><div slot=content"></div><div aria-描述的=o-tooltip-0"><h1 class="chiclet--app-title";data-se=app-card-title">Anaplan</h1>

</o-工具提示></页脚></文章></a>

我抓住了 Anaplan 按钮的 Xpath,它显示了以下内容:

/html[@class='hydrated wf-proximanova-n4-inactive wf-不活动']/body[@class='default']/div[@id='root']/div[@class='enduser-app']/section[@class='content-frame']/main[@class='main-container has-top-bar']/div[@class='dashboard--main']/section[@id='main-内容']/部分[@class='chiclet-area']/section[@class='chiclet-grid--container']/section/section[@class='chiclet-grid section-appear-done section-enter-done']/a[@class='chiclet a--no-decoration'][1]/article[@class='chiclet--article']

应用程序之间的唯一区别是括号中的数字:/a[@class='chiclet a--no-decoration'][1],其中 1 似乎是 Anaplan,3 是 G Drive,依此类推.有没有办法选择这样的元素,其中似乎根本没有唯一标识符?

解决方案

要定位第一个按钮,您可以使用以下 xpaths //a[@aria-label='launch app Anaplan']//a[contains(@href,'anaplan')] 以及许多其他独特的组合.使用 css 选择器
也可以做到这一点与上述类似,您在此处提供的所有其他导航按钮也有多种组合.
如果元素位于