使用Selenium进行JavaScript元素的刮取尝试

时间:2019-03-24 17:22:03

标签: javascript selenium scrapy

我是硒和硒的新手。我要抓取的页面在按钮上使用js脚本进入下一页。我在SO上找到了一部分代码(Click a Button in Scrapy),但我似乎无法使其正常工作。

from selenium import webdriver

import scrapy

class chSpider(scrapy.Spider):
    name = 'spidypy'
    allowed_domains = ['117.145.177.252']
    start_urls = ['http://117.145.177.252/login.do?method=enterPdamccx']

    def __init__(self):
        self.driver = webdriver.Firefox()

    def parse(self,response):

        self.driver.get('http://117.145.177.252/login.do?method=enterPdamccx')

        while True:
            try:
                next = self.driver.find_element_by_xpath('/html/body/form/div[3]/div/div/a')
                url = 'http://117.145.177.252/login.do?method=enterPdamccx'
                yield Request(url,callback=self.parse2)
                next.click()
            except:
                break

        self.driver.close()

    def parse2(self,response):
        print('you are here!')

我多次收到以下错误消息:

selenium.common.exceptions.WebDriverException: Message: connection refused

1 个答案:

答案 0 :(得分:0)

a具有onclick,因此您可以这样做:

driver.execute_script('doMccx()')