In this scrapy I want to click on the goto store open the url in new tab capture the url and close in and move on to the original tab. But the script is throwing error.
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import Selector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from selenium import webdriver
from urlparse import urljoin
import time
from selenium.webdriver.common.keys import Keys
class CompItem(scrapy.Item):
model_name = scrapy.Field()
model_link = scrapy.Field()
url =scrapy.Field()
class criticspider(CrawlSpider):
name = "extract"
allowed_domains = ["mysmartprice.com"]
start_urls = ["http://www.mysmartprice.com/computer/lenovo-g50-70-laptop-msf201821"]
def __init__(self, *args, **kwargs):
super(criticspider, self).__init__(*args, **kwargs)
self.download_delay = 0.25
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(20)
def parse_start_url(self, response):
self.browser.get(response.url)
item = CompItem()
time.sleep(10)
items = []
# Save the window opener (current window, do not mistaken with tab... not the same)
button = self.browser.find_element_by_xpath("/html/body/div[3]/div/div[3]/div/div[2]/div[4]/div[4]/div[5]/div[1]")
main_window = self.browser.current_window_handle
# Open the link in a new tab by sending key strokes on the element
# Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack
button.send_keys(Keys.CONTROL + Keys.RETURN)
# Switch tab to the new tab, which we will assume is the next one on the right
self.browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
time.sleep(10)
# Put focus on current window which will, in fact, put focus on the current visible tab
self.browser.switch_to_window(main_window)
item['url'] = self.browser.current_url
# do whatever you have to do on this page, we will just got to sleep for now
time.sleep(2)
# Close current tab
self.browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
yield item
The code is not throwing any error and I have tried using in multiple browsers. But couldn't found whats wrong?