【问题标题】:Python Selenium Cannot find element using absolute xpath divPython Selenium 找不到使用绝对 xpath div 的元素
【发布时间】:2019-10-14 21:53:43
【问题描述】:

我已经阅读了一天多的网页和 stackoverflow 帖子,我已经到了“大部分”可以点击动态 xPath 但它没有点击的地步......

目标是从一个私人指标的 tradingview 邀请列表中动态删除用户。单击管理访问会创建一个弹出窗口,在其中输入或删除用户。我可以很好地添加用户,但是尝试单击列表中用户名旁边的“x”确实很困难,因为嵌套的 div 是相同的,除了自定义数据用户名标签和跨度文本值。

简而言之,我可以获得绝对 xpath,然后尝试单击它,但我得到一个找不到元素,我知道这是不正确的。

删除用户参数

unsubscribed - 传递给从电视中删除的用户列表
htmlsource - self.driver.page_source 传递给每个用户删除按钮获取 xpath。

设置

Python 3.7.3+ 硒 美丽汤

错误信息

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/MG_TV_Users/tv_monitor.py", line 493, in <module>
    start = TV(headless=False, browser='firefox',logintype='auto')
  File "C:/Users/user/PycharmProjects/MG_TV_Users/tv_monitor.py", line 91, in __init__
    self.update_tv(headless=headless, username=self.username, password=self.password, browser=self.browser)
  File "C:/Users/user/PycharmProjects/MG_TV_Users/tv_monitor.py", line 120, in update_tv
    self.manage_users(google_sheet_user_list)
  File "C:/Users/user/PycharmProjects/MG_TV_Users/tv_monitor.py", line 354, in manage_users
    self.removeusers(removeoldusers, htmlsource)
  File "C:/Users/user/PycharmProjects/MG_TV_Users/tv_monitor.py", line 422, in removeusers
    self.driver.find_element(By.XPATH,UNSUBSCRIBEUSER_BTN).click()
  File "C:\Users\user\PycharmProjects\MG_TV_Users\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\user\PycharmProjects\MG_TV_Users\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\PycharmProjects\MG_TV_Users\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[11]/div/div/div[2]/div/div[2]/div[2]/div[1]/div/div[1]/div[2]/div

删除用户的功能

def removeusers(self, unsubscribed, htmlsource):
    import re
    tv_users = []
    names = BS(htmlsource, features="html.parser")
    result = names.find_all("div", {"class": "tv-manage-access-dialog__user-list-row"})
    for res in result:
        #print(res)
        tv_users.append(res.text)

        #print(elem)


    tempsave = '/html/body/div[11]/div/div/div[2]/div/div[2]/div[2]/div[1]/div/div[1]/div[2]/div'

    #PARENT DIV TO ALL USERS'tv-manage-access-dialog__user-list-body js-user-list'
    tv_user_list = '/html/body/div[11]/div/div/div[2]/div/div[2]/div[2]/div[1]/div'

    print('working on arviman')
    #tempsave = '/html/body/div[11]/div/div/div[2]/div/div[2]/div[2]/div[1]/div/div[1]/div[2]/div'
    for user in result:
        if user.text in unsubscribed:
            elem = names.find(string=re.compile(user.text))
            UNSUBSCRIBED_ROOT_XPATH = self.xpath_soup(elem)
            tostrip = '/div[1]/span' #GOES UP 2 ELEMENTS FROM SPAN NAME TO ROOT DIV OF USER.
            toappend = '/div[2]/div' # LOCATION OF BUTTON 'X' TO REMOVE USER
            if UNSUBSCRIBED_ROOT_XPATH.endswith(tostrip):
                UNSUBSCRIBED_ROOT_XPATH = UNSUBSCRIBED_ROOT_XPATH.replace(tostrip, '')
                UNSUBSCRIBEUSER_BTN = UNSUBSCRIBED_ROOT_XPATH + toappend
                self.driver.find_element(By.XPATH,UNSUBSCRIBEUSER_BTN).click()
                print(user.text , "REMOVED")

管理访问弹出窗口 - 列表

<div class="tv-dialog js-dialog tv-dialog--popup i-focused ui-draggable" tabindex="-1" style="width: calc(100% - 20px); max-width: 500px; top: 459px; left: 265px; z-index: 112;">
   <div class="tv-dialog__section tv-dialog__section--title js-dialog__drag tv-dialog__grab ui-draggable-handle">
      <div class="js-title-text tv-dialog__title">Manage Pine Script Access</div>
   </div>
   <div>
      <div>
         <div class="tv-dialog__section tv-manage-access-dialog__section js-search-placeholder">
            <div class="tv-search-row">
               <input class="tv-search-row__input js-input-control" type="text" name="q" value="" autocomplete="off" data-role="search" placeholder="Add user">
               <span class="tv-search-row__input-reset i-hidden js-reset-button">
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 95.939 95.939" width="95.939" height="95.939">
                     <path d="M62.82 47.97l32.53-32.534a2 2 0 0 0 0-2.828L83.332.586a2 2 0 0 0-2.827 0L47.97 33.12 15.435.587c-.75-.75-2.078-.75-2.828 0L.587 12.607a2 2 0 0 0 0 2.83L33.12 47.97.588 80.504a2 2 0 0 0 0 2.828l12.02 12.02a1.997 1.997 0 0 0 2.83 0L47.97 62.818l32.535 32.535a2 2 0 0 0 2.827 0l12.02-12.02c.78-.783.78-2.05 0-2.83L62.82 47.97z"></path>
                  </svg>
               </span>
               <span class="tv-search-row__search-icon">
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" width="18px" height="18px">
                     <path fill-rule="evenodd" d="M12.5 11h-.79l-.28-.27A6.47 6.47 0 0 0 13 6.5 6.5 6.5 0 1 0 6.5 13c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L17.49 16l-4.99-5zm-6 0C4.01 11 2 8.99 2 6.5S4.01 2 6.5 2 11 4.01 11 6.5 8.99 11 6.5 11z"></path>
                  </svg>
               </span>
               <div class="tv-username-hint-list js-hidden tv-username-hint-list--full-border" style="left: 0px; top: 100%; width: 65%; z-index: 100;"></div>
            </div>
         </div>
         <div class="tv-dialog__section tv-manage-access-dialog__section">
            <div class="tv-text tv-manage-access-dialog__user-list-header js-user-list-header">14 users have access</div>
            <div class="tv-manage-access-dialog__user-list-container tv-dialog__scroll-wrap js-dialog__scroll-wrap wrapper-2KWBfDVB- sb-scroll-active" style="">
               <div class="tv-dialog__scroll-wrap-inner js-user-list-attach" style="bottom: auto; top: 0px;">
                  <div class="tv-manage-access-dialog__user-list-body js-user-list">
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="snub-fighter">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%2890,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3ES%3C/text%3E%3C/svg%3E"><span>snub-fighter</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="KingThies_">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="https://s3.tradingview.com/userpics/1662357-tYZf.png"><span>KingThies_</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="Eniesee_ldk">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%28312,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3EE%3C/text%3E%3C/svg%3E"><span>Eniesee_ldk</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="sugarjun">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%289,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3ES%3C/text%3E%3C/svg%3E"><span>sugarjun</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="dijkie">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%28144,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3ED%3C/text%3E%3C/svg%3E"><span>dijkie</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="PaulAdelaar">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%2884,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3EP%3C/text%3E%3C/svg%3E"><span>PaulAdelaar</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="ladedimone">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%28102,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3EL%3C/text%3E%3C/svg%3E"><span>ladedimone</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="DK32100">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%28339,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3ED%3C/text%3E%3C/svg%3E"><span>DK32100</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="Pisonglobal">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%28126,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3EP%3C/text%3E%3C/svg%3E"><span>Pisonglobal</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="whiteltr">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%2893,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3EW%3C/text%3E%3C/svg%3E"><span>whiteltr</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="nerf_herder">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="https://s3.tradingview.com/userpics/3591101-9n72.png"><span>nerf_herder</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="Swaine123">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="https://s3.tradingview.com/userpics/4117895-HxYz.png"><span>Swaine123</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="Astynaxe">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="https://s3.tradingview.com/userpics/4207689-Eli2.png"><span>Astynaxe</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                     <div class="tv-manage-access-dialog__user-list-row js-user-row" data-username="BassieWouters">
                        <div class="tv-manage-access-dialog__user-info-column js-userlink-popup"><img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220,0,20,20%22%20width=%2239%22%20height=%2239%22%3E%3Crect%20height=%2220%22%20width=%2220%22%20fill=%22hsl%28120,25%25,50%25%29%22/%3E%3Ctext%20fill=%22white%22%20x=%2210%22%20y=%2214.8%22%20font-size=%2214%22%20font-family=%22Trebuchet%20MS,Arial,sans-serif%22%20text-anchor=%22middle%22%3EB%3C/text%3E%3C/svg%3E"><span>BassieWouters</span></div>
                        <div class="tv-manage-access-dialog__user-remove-column">
                           <div class="apply-common-tooltip tv-manage-access-dialog__user-remove-btn js-user-remove-btn" title="Remove access">
                              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10">
                                 <g fill="none" stroke="#758696" stroke-width="2">
                                    <path d="M1.125 9.11L9.13 1.104M1.125 1.105L9.13 9.11"></path>
                                 </g>
                              </svg>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
               <div class="sb-inner-shadow top i-invisible"></div>
               <div class="sb-inner-shadow"></div>
               <div class="sb-scrollbar-wrap">
                  <div class="sb-scrollbar sb-scrollbar-body ui-draggable ui-draggable-handle" style="height: 317.46px; top: 1px;"></div>
               </div>
            </div>
         </div>
      </div>
   </div>
   <div class="tv-dialog__close js-dialog__close">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 13 13" width="13" height="13">
         <path d="M5.182 6.596L1.293 2.707.586 2 2 .586l.707.707 3.889 3.889 3.889-3.889.707-.707L12.606 2l-.707.707L8.01 6.596l3.889 3.889.707.707-1.414 1.414-.707-.707L6.596 8.01l-3.889 3.889-.707.707-1.414-1.414.707-.707 3.889-3.889z"></path>
      </svg>
   </div>
</div>

编辑

根据要求,我已尝试进行此更改。

        for user in result:
            if user.text in unsubscribed:
                    self.driver.find_element_by_css_selector(f"[data-username='{user.text}'] svg").click()
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [data-username='snub-fighter'] svg

【问题讨论】:

  • 请检查元素前是否存在 iframe
  • 据我所见 - 我确实检查了我没有看到与此弹出窗口相关的 iframe。

标签: python html selenium xpath


【解决方案1】:

这很奇怪,你在那里尝试做什么。尝试一些简单的东西,比如下面的代码:

user_list = ['ladedimone', 'PaulAdelaar', 'Swaine123']

# you're on page with unsubscribe list using Selenium

for user in user_list:
    driver.find_element_by_css_selector(f"[data-username='{user}'] svg").click()

【讨论】:

  • 首先,谢谢。我只是按照用户的建议尝试了这个结果:如果 user.text in unsubscribed: self.driver.find_element_by_css_selector(f"[data-username='{user.text}'] svg").click() selenium.common。 exceptions.NoSuchElementException:消息:无法找到元素:[data-username='snub-fighter'] svg`
  • 从页面共享完整的 HTML。更好的网址
  • 我不能添加超过 30000。如果需要我可以将您的帐户添加到页面中,如果需要,我会展开上面的 html。正文限制为 30000 个字符;你输入了 1271738。 URL = tradingview.com/script/fY4QMhMJ-test-script-zerpgames
  • 不应该有div前缀​​ driver.find_element_by_css_selector(f"[data-username='{user.text'] svg").click()
  • 提供一个用户和步骤
【解决方案2】:

花了一些时间,但这里是解决方案。

def removeusers(self, unsubscribed, htmlsource):
    #print("Missing users in Unsubscribed list:",unsubscribed)
    wait(self.driver, 6).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Manage Access']"))).click()
    time.sleep(3)
    for name in unsubscribed:
        #print( "//div[@data-username='{}']//div[contains(@class,'tv-manage-access-dialog__user-remove-btn')]".format(name))
        try:
            self.driver.find_element_by_xpath(
                "//div[@data-username='{}']//div[contains(@class,'tv-manage-access-dialog__user-remove-btn')]".format(
                    name)).click()
            print("REMOVED : ",name)

        except Exception as e:
            print(e)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2013-05-21
    相关资源
    最近更新 更多