簡體   English   中英

我如何使用 Python 和 Selenium 遍歷 webelements 列表?

[英]How do i iterate through a webelements list with Python and Selenium?

我想遍歷 webelement 列表並從每個列表中返回文本,但我只從第一個<h2>元素中獲取文本,而不是從其他<li>標簽內的其余元素中獲取文本,然后代碼存在那個循環

這是我想從中提取文本的 Html 代碼的一部分:

 <div class="KambiBC-event-page-component__column KambiBC-event-page-component__column--1"> <ul class="KambiBC-list-view__column"> <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in"> <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true"> <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Piete selectate</h2> </header> </li> <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in"> <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true"> <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Another text</h2> </header> </li> <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in"> <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true"> <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Different text</h2> </header> </li> <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in"> <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true"> <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Yet another text</h2> </header> </li> </ul> </div>

這是 Pyhton 代碼:

 import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Edge("D:\\pariuri\\python\\MicrosoftWebDriver.exe") driver.implicitly_wait(5) driver.get("https://www.unibet.ro/betting#filter/football") try: element_present = EC.presence_of_element_located((By.CLASS_NAME, 'KambiBC-event-result__score-list')) WebDriverWait(driver, 4).until(element_present) except TimeoutException: print ('Timed out waiting for page to load') event = driver.find_elements_by_class_name('KambiBC-event-item KambiBC-event-item--type-match') for items in event: link = items.find_element_by_class_name('KambiBC-event-item__link') scoruri = items.find_element_by_class_name('KambiBC-event-item__score-container') scor1 = scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[1]") scor2 = scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[2]") print (scor1.text) print (scor2.text) if scor1.text == '0' and scor2.text == '0': link.click() time.sleep(3) PlajePariuri = driver.find_elements_by_xpath("//ul[@class='KambiBC-list-view__column']") for items in PlajePariuri: NumePlaje = items.find_element_by_xpath("//li/header/h2") print (NumePlaje.text)

它一直在我的臉上,這將打印每個元素的文本,很高興我能找到

PlajePariuri = driver.find_elements_by_class_name('KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in')


    for items2 in PlajePariuri:

        NumePlaje = items2.find_element_by_class_name('KambiBC-bet-offer-category__title js-bet-offer-category-title')

        print (NumePlaje.text)

試試下面的代碼-

PlajePariuri = driver.find_elements_by_xpath("//ul[@class='KambiBC-list-view__column']//li/header/h2")
for items in PlajePariuri:
    print (items.text)

不要使用classname定位器,而是嘗試使用xpath ,如下所示:

PlajePariuri = driver.find_elements_by_xpath("//ul[@class='KambiBC-list-view__column']")
for items in PlajePariuri:
    NumePlaje = items.find_element_by_xpath("//li/header/h2")
    print (NumePlaje.text)

我做了一個實現來查找列表中的元素。

我的情況是我們有一個帶有側列表的 wiki,該列表中可能有或沒有列表等等。 這是我的解決方案:

// #Create a function to receive the old HTML (Before click), 
// #new HTML (After click), and the element I'm looking for:

def page_handler(old_source,new_source,element):
    new_content = []

    // #Put page into a list (need to verify if it works for you)
    old_page = old_source.split('\n')
    new_page = new_source.split('\n')

    // #Compare the old page and new page. The content of the new page, I check if 
    // #matches with the element I'm looking for

    for data in new_page:
        if data not in old_page:
            if element in data:
                new_content.append(data)

    return new_content

// #Now in the main thread, before the program Click on the item, take a snapshot:
old_page = driver.page_source

// #Click on the item
elem = driver.find_element_by_link_text(item).click()

// #take a new snapshot
new_page = driver.page_source

// # Use the function to send the old page and new page, and the class you are looking 
// #for in the HTML code:
new_pg_data = page_handler(old_page,new_page,'class="plugin_pagetree_children_span"')

// # Now I have the children elements, just iterate the list.

for element_id in new_pg_data:
    // #I use regexp to get the element ID
    element_id = search('id="(.*)">  ',element_id)
    if element_id:
        elem = driver.find_element_by_id(element_id).click()

我希望這個解決方案可以幫助你們。

暫無
暫無

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

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