如何选择动态元素以及如何使用 aria-label 使用 selenium 和 python 选择元素
How to pick dynamic element and how to pick element using aria-label using selenium and python
这是我试图从中挑选两个元素的网页快照,一个是用于查看隐藏选项的更多按钮,另一个是点击可见时点击隐藏选项。
<div id="ember54" class="artdeco-dropdown artdeco-dropdown--placement-bottom artdeco-dropdown--justification-left ember-view">
<button aria-expanded="false" id="ember55" class="artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view
pvs-profile-actions__action
artdeco-button
artdeco-button--secondary
artdeco-button--muted
artdeco-button--2
" type="button" tabindex="0">
<span aria-label="More actions">More</span>
<!----></button>
<div tabindex="-1" aria-hidden="true" id="ember56" class="pvs-overflow-actions-dropdown__content artdeco-dropdown__content artdeco-dropdown--is-dropdown-element artdeco-dropdown__content--justification-left artdeco-dropdown__content--placement-bottom ember-view"><div class="artdeco-dropdown__content-inner">
<ul>
<li>
<div data-control-name="share-profile" id="ember57" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="share-linkedin-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M23 12l-4.61 7H16l4-6H8a3.92 3.92 0 00-4 3.84V17a4 4 0 00.19 1.24L5.12 21H3l-.73-2.22A6.4 6.4 0 012 16.94 6 6 0 018 11h12l-4-6h2.39z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Share profile in a message</span>
<span class="a11y-text">Share Mr X’s profile via message</span>
</div>
</li>
<li>
<div data-control-name="save_to_pdf" id="ember58" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="download-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M21 14v5a3 3 0 01-3 3H6a3 3 0 01-3-3v-5h2v5a1 1 0 001 1h12a1 1 0 001-1v-5zm-4-.57V11l-4 2.85V3h-2v10.85L7 11v2.43L12 17z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Save to PDF</span>
<span class="a11y-text">Save Mr X’s profile to PDF</span>
</div>
</li>
<li>
<div data-control-name="follow" id="ember59" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="plus-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M21 13h-8v8h-2v-8H3v-2h8V3h2v8h8z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Follow</span>
<span class="a11y-text">Follow Mr X</span>
</div>
</li>
<li>
<div data-control-name="connect" id="ember60" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="connect-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M14 7a5 5 0 11-5-5 5 5 0 015 5zm-3 7H7a3 3 0 00-3 3v5h10v-5a3 3 0 00-3-3zm9-2V9h-2v3h-3v2h3v3h2v-3h3v-2z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Connect</span>
<span class="a11y-text">Connect with Mr X</span>
</div>
<!---->
</li>
</ul>
</div>
</div>
</div>
其中 id="emberx" 不断变化。
我正在尝试选择元素 - <span aria-label="More actions">More</span>
通过
driver.find_element_by_class_name('artdeco-dropdown artdeco-dropdown--placement-bottom artdeco-dropdown--justification-left ember-view">
<button aria-expanded="false" id="ember55" class="artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view
pvs-profile-actions__action
artdeco-button
artdeco-button--secondary
artdeco-button--muted
artdeco-button--2').find_element_by_tag_name('span').click()
但它给我一个错误 no such element: Unable to locate element
我也在尝试拉取元素 data-control-name="connect" id="ember60"
但那个 id 也在不断变化,
这个ember#在同一个网页上出现了数百次。
你知道吗,我们该如何处理?
使用下面的 xpath :
//span[text()='More']/..
然后像这样点击它:
driver.find_element_by_xpath('//span[text()='More']/..').click()
如果 emberx
不断变化,你可以这样写 xpath :
//div[contains(@id, ember) and contains(@class, 'display-flex align-items-center')][1]
或者没有任何效果,您可以使用 find_elements
在列表中获取所有 100 个余烬
for ember in driver.find_elements(By.XPATH, "//div[contains(@id, ember) and contains(@class, 'display-flex align-items-center')]")
print(ember.get_attribute('innerHTML'))
这是我试图从中挑选两个元素的网页快照,一个是用于查看隐藏选项的更多按钮,另一个是点击可见时点击隐藏选项。
<div id="ember54" class="artdeco-dropdown artdeco-dropdown--placement-bottom artdeco-dropdown--justification-left ember-view">
<button aria-expanded="false" id="ember55" class="artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view
pvs-profile-actions__action
artdeco-button
artdeco-button--secondary
artdeco-button--muted
artdeco-button--2
" type="button" tabindex="0">
<span aria-label="More actions">More</span>
<!----></button>
<div tabindex="-1" aria-hidden="true" id="ember56" class="pvs-overflow-actions-dropdown__content artdeco-dropdown__content artdeco-dropdown--is-dropdown-element artdeco-dropdown__content--justification-left artdeco-dropdown__content--placement-bottom ember-view"><div class="artdeco-dropdown__content-inner">
<ul>
<li>
<div data-control-name="share-profile" id="ember57" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="share-linkedin-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M23 12l-4.61 7H16l4-6H8a3.92 3.92 0 00-4 3.84V17a4 4 0 00.19 1.24L5.12 21H3l-.73-2.22A6.4 6.4 0 012 16.94 6 6 0 018 11h12l-4-6h2.39z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Share profile in a message</span>
<span class="a11y-text">Share Mr X’s profile via message</span>
</div>
</li>
<li>
<div data-control-name="save_to_pdf" id="ember58" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="download-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M21 14v5a3 3 0 01-3 3H6a3 3 0 01-3-3v-5h2v5a1 1 0 001 1h12a1 1 0 001-1v-5zm-4-.57V11l-4 2.85V3h-2v10.85L7 11v2.43L12 17z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Save to PDF</span>
<span class="a11y-text">Save Mr X’s profile to PDF</span>
</div>
</li>
<li>
<div data-control-name="follow" id="ember59" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="plus-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M21 13h-8v8h-2v-8H3v-2h8V3h2v8h8z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Follow</span>
<span class="a11y-text">Follow Mr X</span>
</div>
</li>
<li>
<div data-control-name="connect" id="ember60" class="pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view" tabindex="0"><!---->
<li-icon aria-hidden="true" type="connect-icon" class="mr3" size="large"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" class="mercado-match" width="24" height="24" focusable="false">
<path d="M14 7a5 5 0 11-5-5 5 5 0 015 5zm-3 7H7a3 3 0 00-3 3v5h10v-5a3 3 0 00-3-3zm9-2V9h-2v3h-3v2h3v3h2v-3h3v-2z"></path>
</svg></li-icon>
<span class="display-flex t-normal" aria-hidden="true">Connect</span>
<span class="a11y-text">Connect with Mr X</span>
</div>
<!---->
</li>
</ul>
</div>
</div>
</div>
其中 id="emberx" 不断变化。
我正在尝试选择元素 - <span aria-label="More actions">More</span>
通过
driver.find_element_by_class_name('artdeco-dropdown artdeco-dropdown--placement-bottom artdeco-dropdown--justification-left ember-view">
<button aria-expanded="false" id="ember55" class="artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view
pvs-profile-actions__action
artdeco-button
artdeco-button--secondary
artdeco-button--muted
artdeco-button--2').find_element_by_tag_name('span').click()
但它给我一个错误 no such element: Unable to locate element
我也在尝试拉取元素 data-control-name="connect" id="ember60"
但那个 id 也在不断变化,
这个ember#在同一个网页上出现了数百次。
你知道吗,我们该如何处理?
使用下面的 xpath :
//span[text()='More']/..
然后像这样点击它:
driver.find_element_by_xpath('//span[text()='More']/..').click()
如果 emberx
不断变化,你可以这样写 xpath :
//div[contains(@id, ember) and contains(@class, 'display-flex align-items-center')][1]
或者没有任何效果,您可以使用 find_elements
for ember in driver.find_elements(By.XPATH, "//div[contains(@id, ember) and contains(@class, 'display-flex align-items-center')]")
print(ember.get_attribute('innerHTML'))