【发布时间】:2021-07-26 17:11:33
【问题描述】:
我的目标是打开一个网页,然后单击特定应用的应用按钮,例如 Anaplan。过去,我使用 CSS 选择器获取元素,结合类和 ID,as shown in this past post。
first_item = driver.find_element_by_id("anaplan")
我遇到了一个网页,其中的按钮似乎没有任何 ID 或唯一值:
Anaplan App 按钮的 HTML 输出:
<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>
我抓取了 Anaplan 按钮的 Xpath,显示如下:
/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']
应用程序之间的唯一区别是括号中的数字:
/a[@class='chiclet a--no-decoration'][1],其中 1 似乎是 Anaplan,3 是 G Drive,依此类推。有没有办法选择像这样似乎根本没有唯一标识符的元素?
【问题讨论】:
标签: python python-3.x selenium web-scraping