简体   繁体   English

如何在使用 python selenium 通过 id 调用查找元素之前解析 id 号?

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

I'm trying to automate linkedin connection request using python selenium but problem is every time the value of ember number keeps changing.我正在尝试使用 python selenium 自动执行linkedin连接请求,但问题是每次余烬编号的值都在不断变化。 when I try to copy id of button It gives certain string one time // [@id="ember418"] and other time it gives // [@id="ember502"] so the number keeps changing is there a way to find this number before passing id value??当我尝试复制按钮的 id 它一次给出某个字符串 // [@id="ember418"] 其他时间给出 // [@id="ember502"] 所以数字不断变化有没有办法找到这个数字在传递 id 值之前?

this is the complete element tree I'm working on这是我正在处理的完整元素树

<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>

I'm trying to click add a note and send buttons by finding the id我正在尝试通过查找 id 单击添加注释和发送按钮

when I call当我打电话时

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

it will click that button它会点击那个按钮

but problem is every time it get refreshed or reclicked ember number keeps changing但问题是每次刷新或重新单击时,余烬编号都会不断变化

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"ember524"} (Session info: chrome=90.0.4430.93) NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“id”,“selector”:“ember524”}(会话信息:chrome = 90.0.4430.93)

so I want to fetch this ember number before finding the element by id所以我想在通过 id 找到元素之前获取这个 ember 编号

is there a way to achieve this?有没有办法做到这一点? any help will be grateful任何帮助将不胜感激

You can use the following xpath to select that element:您可以使用以下xpath到 select 那个元素:
//button[contains(@class,'primary') and contains(@class,'profile-actions--connect')]

Try this xpath.//button[@aria-label='Add a note']试试这个 xpath.//button[@aria-label='Add a note']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM