Вы можете пройти через всю строку кода, я уверен, что вы получите ответ. это позволяет легко и надежно настроить таргетинг на элемент в xpath
Начальная настройка
from selenium import webdriver
wb = webdriver.Chrome('Path to your chrome webdriver')
wb.get('https://na.op.gg/champion/statistics')
Для уровня TOP
y_top = {}
tbody_top = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr')
for i in range(len(tbody_top)):
y_top[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-TOP"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')
для джунглей
y_jung = {}
tbody_jung = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-JUNGLE"]/tr')
for i in range(len(tbody_jung)):
y_jung[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-JUNGLE"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-JUNGLE"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')
для средней
y_mid = {}
tbody_mid = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-MID"]/tr')
for i in range(len(tbody_mid)):
y_mid[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-MID"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-MID"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')
для нижней
y_bott = {}
tbody_bott = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-ADC"]/tr')
for i in range(len(tbody_bott)):
y_bott[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-ADC"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-ADC"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')
для поддержки
y_sup = {}
tbody_sup = wb.find_elements_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-SUPPORT"]/tr')
for i in range(len(tbody_sup)):
y_sup[wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-SUPPORT"]/tr['+str(i+1)+']/td[4]/a/div[1]').text] = wb.find_element_by_xpath('//table[@class = "champion-index-table tabItems"]/tbody[@class="tabItem champion-trend-tier-SUPPORT"]/tr['+str(i+1)+']/td[5]').text.rstrip('%')
print(max(y_top,key = y_top.get)) # it will print the max win rate for TOP tier
print(max(y_jung,key = y_jung.get))
print(max(y_mid,key = y_mid.get))
print(max(y_bott,key = y_bott.get))
print(max(y_sup,key = y_sup.get))
=========================================
Вы можете выбрать любой элемент, используя его атрибут xpath, как показано ниже:
1. wb.find_element_by_xpath('//div[@id="the_id_of_div"]/a/span')
2. wb.find_element_by_xpath('//div[@class="class name"]/p/span')
3. wb.find_element_by_xpath('//div[@title="title of element"]/p/span')
4. wb.find_element_by_xpath('//div[@style="style x "]/p/span')
5. wb.find_elements_by_xpath('//*[contains(text(),"the text u wanna find")]') #may be the page has multiple same text that u wanna search...keep in mind
6. найти родителя найденного элемента ==>
that_found_element.find_element_by_xpath('..') # and you can iterate it to the top most parent using loop
7. найти родственника элемента ===>
, чтобы найти предыдущий элемент
wb.find_element_by_xpath('//span[@id ="theidx"]//preceding-sibling::input') #this tells target a input tag which is preceding sibling of span tag with id as "theidx"
, чтобы найти следующий элемент
wb.find_element_by_xpath('//span[@id ="theidy"]//following-sibling::input') #this tells target a input tag which is following sibling of span tag with id as "theidy"