簡體   English   中英

如何在使用 python selenium 通過 id 調用查找元素之前解析 id 號?

[英]How to parse id number before calling find element by id using python selenium?

我正在嘗試使用 python selenium 自動執行linkedin連接請求,但問題是每次余燼編號的值都在不斷變化。 當我嘗試復制按鈕的 id 它一次給出某個字符串 // [@id="ember418"] 其他時間給出 // [@id="ember502"] 所以數字不斷變化有沒有辦法找到這個數字在傳遞 id 值之前?

這是我正在處理的完整元素樹

<div data-test-modal="" role="dialog" tabindex="-1" class="artdeco-modal artdeco-modal--layer-default send-invite" size="medium" aria-labelledby="send-invite-modal">
        <span class="a11y-text">Dialog content start.</span>
        <button data-test-modal-close-btn="" aria-label="Dismiss" id="ember414" class="artdeco-modal__dismiss artdeco-button artdeco-button--circle artdeco-button--muted artdeco-button--2 artdeco-button--tertiary ember-view">  <li-icon aria-hidden="true" type="cancel-icon" class="artdeco-button__icon"><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="M13.42 12L20 18.58 18.58 20 12 13.42 5.42 20 4 18.58 10.58 12 4 5.42 5.42 4 12 10.58 18.58 4 20 5.42z"></path>
</svg></li-icon>

<span class="artdeco-button__text">
    
</span></button>
        
      <div id="ember415" class="artdeco-modal__header ember-view">
        <h2 id="send-invite-modal">
            Your invitation is almost on its way
        </h2>
      </div>
      <div id="ember416" class="artdeco-modal__content ember-view">
<!----><!---->
            <p class="display-flex">
                <li-icon aria-hidden="true" type="success-pebble-icon" class="connect-button-send-invite__success-icon mr1" 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="M12 2a10 10 0 1010 10A10 10 0 0012 2zm-1.25 15L7 13.25l1.41-1.41L10.59 14l4.84-6H18z"></path>
</svg></li-icon>
                <span class="flex-1">
                  You can add a note to personalize your invitation to <strong>Zoe Yang</strong>.
                </span>
            </p>
<!---->
      </div>
      <div id="ember417" class="artdeco-modal__actionbar text-align-right ember-view">
          <button aria-label="Add a note" id="ember418" class="mr1 artdeco-button artdeco-button--muted artdeco-button--3 artdeco-button--secondary ember-view"><!---->
<span class="artdeco-button__text">
    Add a note
</span></button>
        <button aria-label="Send now" id="ember419" class="ml1 artdeco-button artdeco-button--3 artdeco-button--primary ember-view"><!---->
<span class="artdeco-button__text">
    Send
</span></button>
      </div>
      
        <span class="a11y-text">Dialog content end.</span>
      </div>

我正在嘗試通過查找 id 單擊添加注釋和發送按鈕

當我打電話時

connect = more = driver.find_element_by_id("""ember418""").click()

它會點擊那個按鈕

但問題是每次刷新或重新單擊時,余燼編號都會不斷變化

NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{“method”:“id”,“selector”:“ember524”}(會話信息:chrome = 90.0.4430.93)

所以我想在通過 id 找到元素之前獲取這個 ember 編號

有沒有辦法做到這一點? 任何幫助將不勝感激

您可以使用以下xpath到 select 那個元素:
//button[contains(@class,'primary') and contains(@class,'profile-actions--connect')]

試試這個 xpath.//button[@aria-label='Add a note']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM