【问题标题】:Selenium Web Scraping returns error when Clicking on multiple button on one page单击一页上的多个按钮时,Selenium Web Scraping 返回错误
【发布时间】:2018-07-23 12:11:14
【问题描述】:

真的需要这个社区的帮助!

当我尝试从旅游网站上抓取动态内容时,只有单击网站上的“查看价格”按钮才能获取价格和相关供应商信息。因此,我正在考虑在使用 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。附图为网页截图。

非常感谢社区的帮助!

【问题讨论】:

标签: python selenium dynamic web-scraping


【解决方案1】:

您可能需要为您正在使用的 Xpath 选择相对路径。 在您执行数据时,显示的数据可能只是部分存在。

尝试方法:

  1. 增加等待时间
  2. 更改 xpath / 使用相对 xpath
  3. Splinter - 你可以用它作为浏览器调用的常规方式

在进行调用时,您需要检查数据是否存在于 DOM 元素中。如果是这种情况,等到加载完整页面会帮助你。

【讨论】:

  • 我对网络抓取动态内容非常陌生。你能给我举个例子吗!我会很感激的!
  • 我可以检查,您的 xpath 不正确,例如:您的第一个 xpath 在网页上选择 3 个元素。
【解决方案2】:

你好使用下面的代码点击每个价格按钮,如果你愿意也可以引入隐式等待。

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

让我知道您的 BOT 是否能够点击价格按钮

谢谢

快乐编码

【讨论】:

  • 点击了一些按钮(三个按钮或两个按钮),但仍然弹出该错误!我该如何解决?谢谢!
【解决方案3】:

这是根据您的要求设计的功能:

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"]')

【讨论】:

  • 这真的有效!你真是个天才!非常感谢您的帮助
猜你喜欢
  • 2011-04-03
  • 2020-02-11
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
相关资源
最近更新 更多