簡體   English   中英

單擊一頁上的多個按鈕時,Selenium Web Scraping 返回錯誤

[英]Selenium Web Scraping returns error when Clicking on multiple button on one page

真的需要這個社區的幫助!

當我嘗試從旅游網站抓取動態內容時,只有單擊網站上的“查看價格”按鈕才能獲得價格和相關供應商信息。 因此,在使用 Selenium 進行抓取之前,我正在考慮使用“for 循環”來單擊所有“查看價格”按鈕。

問題是每個按鈕都可以通過browser.find_element_by_xpath().click() ,但是當我創建一個包含所有按鈕信息的列表時,會彈出一個錯誤:

代碼塊:

browser=webdriver.Chrome("C:/Users/Owner/Downloads/chromedriver_win32/chromedriver.exe")
url="https://www.cruisecritic.com/cruiseto/cruiseitineraries.cfm?port=122"
browser.get(url)
#print(browser.find_element_by_css_selector(".has-price.meta-link.show-column").text)
ButtonList=[ "//div[@id='find-a-cruise-full-results-container']/div/article/ul/li[3]/span[2]",
             "//div[@id='find-a-cruise-full-results-container']/div/article[2]/ul/li[3]/span[2]",
             "//div[@id='find-a-cruise-full-results-container']/div/article[3]/ul/li[3]/span[2]"]

for button in ButtonList:
    browser.implicitly_wait(20)
    browser.find_element_by_xpath(str(button)).click()
 

錯誤堆棧跟蹤:

WebDriverException: unknown error: Element <span class="label hidden-xs-down" data-title="...">View All Prices</span> is not clickable at point (862, 12). Other element would receive the click: <a href="https://boards.cruisecritic.com" onclick="s_objectID='Primary Nav : Boards';">...</a>
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)

我的問題是如何在抓取之前單擊網頁上的所有按鈕,或者如果我們必須單擊某個按鈕將數據“解析”為 Python,是否還有其他方法可以抓取網頁上的動態內容。 附圖為網頁截圖。

真的很感謝社區的幫助!

在此處輸入圖片說明

您可能需要為您正在使用的 Xpath 選擇相對路徑。 在您執行數據時,顯示的數據可能只是部分存在的情況。

嘗試的方法:

  1. 增加等待時間
  2. 更改 xpath / 使用相對 xpath
  3. Splinter - 您可以將其用作瀏覽器調用的常規方式

您需要在進行調用時檢查數據是否存在於 DOM 元素中。 如果是這種情況,請等待加載完整頁面將幫助您。

您好 使用以下代碼點擊每個價格按鈕,如果您願意,還可以引入隱式等待。

for one_row_view_price in browser.find_elements_by_xpath('//span[@data-title="View All Prices"]'):
     one_row_view_price.click()

讓我知道您的 BOT 是否能夠點擊價格按鈕

謝謝

快樂編碼

這是根據您的要求設計的功能:

def click_handler(xpath):
    # Find total number of element available on webpage
    xpath = re.sub('"', "'", xpath)
    total_element = browser.execute_script("""
                                                        var elements = document.evaluate("%s",
                                                        document,
                                                        null,
                                                        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                                                        null);
                                                        return elements.snapshotLength;
                                                        """ %xpath
                                                        )
    # Check if there is any element
    if(total_element):
    # Iterate over all found elements
        for element_pos in range(total_element):
            # Call click element function
            browser.execute_script("""
                             var elements = document.evaluate("%s",
                             document,
                             null,
                             XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                             null);
                             var im = elements.snapshotItem(%d);
                             im.click();
                             """ %(xpath, element_pos)
                             )
            print "***" + str(element_pos + 1) + " Elements clicked"    
        print "\n****************************"
        print "Total " + str(total_element) + " Elements clicked"
        print "****************************\n"
    # Inform user that there is no element found on webpage
    else:
        print "\n****************************"
        print "Element not found on Webpage"
        print "****************************\n"
        # Element not found on Webpage

click_handler('//span[@data-title="View All Prices"]')

暫無
暫無

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

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