有没有办法在没有唯一 ID 的 Selenium 网页上选择项目? [英] Is there a way to select an item on a webpage with Selenium with no unique ID?
问题描述
我的目标是打开一个网页,然后单击特定应用程序的应用程序按钮,例如 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,依此类推.有没有办法选择这样的元素,其中似乎根本没有唯一标识符?
要定位第一个按钮,您可以使用以下 xpath
s //a[@aria-label='launch app Anaplan']
或 //a[contains(@href,'anaplan')]
以及许多其他独特的组合.使用 css 选择器
也可以做到这一点与上述类似,您在此处提供的所有其他导航按钮也有多种组合.
如果元素位于 内,您必须先切换到该
,然后再退出.
使用
定位iframe = driver.find_element_by_xpath("//iframe[@name='iframeName']")
或它匹配的任何定位器
然后 switch_to
:
driver.switch_to.frame(iframe)
如果之后您需要在 <iframe>
之外的任何地方继续使用 driver.switch_to.default_content()
My objective is to open a webpage, and click the app button for a specific app, like Anaplan. In the past, I've used get element by CSS selector with the combination of class, and ID, as shown in this past post.
first_item = driver.find_element_by_id("anaplan")
I've come across a webpage where the buttons seem to have literally no ID whatsoever, or unique values:
HTML output of the Anaplan App button:
<a
aria-label="launch app Anaplan"
class="chiclet a--no-decoration"
data-se="app-card"
href="https://gartner.okta.com/home/anaplan/0oaforg08lyATdLuw4x6/2487"
draggable="true"
><article class="chiclet--article">
<button
class="chiclet--action"
tabindex="0"
aria-label="Settings for Anaplan"
data-se="app-card-settings-button"
>
<svg
class="chiclet--action-kebab"
width="20"
height="4"
viewBox="0 0 20 4"
fill="#B7BCC0"
xmlns="http://www.w3.org/2000/svg"
>
<circle cx="2" cy="2" r="2"></circle>
<circle cx="10" cy="2" r="2"></circle>
<circle cx="18" cy="2" r="2"></circle>
</svg>
</button>
<section class="chiclet--main" data-se="app-card-main">
<img
class="chiclet--main-logo"
src="https://ok11static.oktacdn.com/fs/bcg/4/gfs1ev15ab63zqgZ91d8"
alt="Anaplan logo"
/>
</section>
<footer class="chiclet--footer" data-se="app-card-footer">
<o-tooltip content="Anaplan" position="bottom" class="hydrated"
><div slot="content"></div>
<div aria-describedby="o-tooltip-0">
<h1 class="chiclet--app-title" data-se="app-card-title">Anaplan</h1>
</div>
</o-tooltip>
</footer>
</article>
</a>
I grabbed the Xpath of the Anaplan button, which shows the following:
/html[@class='hydrated wf-proximanova-n4-inactive wf-
inactive']/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-
content']/section[@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']
The only differences between apps is the number in the bracket:
/a[@class='chiclet a--no-decoration'][1]
, where 1 seems to be Anaplan, 3 is G Drive, and so on. Is there a way to select elements such as this where there appears to be no unique identifier at all?
To locate the first button you can use one of the following xpath
s //a[@aria-label='launch app Anaplan']
or //a[contains(@href,'anaplan')]
and there are many other unique combinations. The same can be done with css selectors
Similarly to the above there are several combinations for all the other navigation buttons you provided here.
In case the element located inside <iframe>
you have to switch to that <iframe>
first and get out of it after that.
Locate the <iframe>
with
iframe = driver.find_element_by_xpath("//iframe[@name='iframeName']")
or whatever locator that it matches
Then switch_to
the <iframe>
:
driver.switch_to.frame(iframe)
If after that you need to continue anywhere out of the <iframe>
switch out of it with
driver.switch_to.default_content()
这篇关于有没有办法在没有唯一 ID 的 Selenium 网页上选择项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!