Я прочитал за несколько дней веб-страниц и сообщений от stackoverflow, и я дошел до того, что я "в основном" получаю динамический xPath, на который можно кликать, но он не щелкает ...
Цель состоит в том, чтобыдинамически удалять пользователя из списка трейд-юв-инвайт только для частного индикатора. Нажатие на Управление доступом создает всплывающее окно, где пользователи вводятся или удаляются. Я могу добавлять пользователей очень хорошо, однако попытка щелкнуть «x» рядом с именем пользователя в списке действительно затруднена из-за идентичности вложенных элементов div, за исключением пользовательского тега data-username и значения текста span.
Короче говоря, я могу получить абсолютный xpath, и я пытаюсь щелкнуть по нему, но я получаю элемент не может найти, который, как я знаю, не соответствует действительности.
Удалить аргументы пользователя
отписался - список пользователей, которые были переданыдля удаления с ТВ
htmlsource - self.driver.page_source
передается для получения xpath для каждой кнопки удаления пользователя.
Настройка
Python 3.7.3+ Selenium BeautifulSoup
СООБЩЕНИЕ ОБ ОШИБКЕ
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")
УПРАВЛЕНИЕ ДОСТУПОМ POP UP WINDOW -СПИСОК
<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