Advertisement
zmzmuazz

Untitled

Sep 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.51 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. from selenium.webdriver.support.ui import Select
  4. import time
  5. from random import randint
  6. from selenium.webdriver.common.keys import Keys
  7. from selenium.common.exceptions import NoSuchElementException
  8. from selenium.common.exceptions import InvalidSelectorException
  9.  
  10.  
  11. SLEEPTIME=1
  12.  
  13. if __name__ == '__main__':
  14.     #if below doesnt work
  15.     driver=webdriver.Chrome()
  16.     driver.maximize_window()
  17.     raw_input("Should I proceed? ")   # add VPN extension before it loads a page in web browser
  18.  
  19.     fileName='output.txt'
  20.     with open("urls2.txt", "r") as f1:
  21.             with open(fileName,'a') as f2:
  22.                 f2.write("Page URL" + "\t" + "Item No." + "\t" + "Description" + "\t" + "Color Name" + "\t" + "Color URL" + "\t" + "Medium Sizes" + '\n')
  23.                 for line in f1:
  24.                         driver.get(line)
  25.                         driver.implicitly_wait(4) #give an implicit wait so that the page gets loaded completely
  26.                         print str(line.split("\n")[0])
  27.  
  28.  
  29.                         try:
  30.                             item_no=driver.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/content-expander/div/div[2]/ng-transclude/div/div[1]/ul/li[1]").get_attribute("content")
  31.                         except NoSuchElementException:
  32.                             item_no="could not find item no"
  33.  
  34.  
  35.                         print str(item_no)
  36.                        
  37.                         description=driver.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/content-expander/div/div[2]/ng-transclude").get_attribute('innerHTML')
  38.                        
  39.                         print str(description.encode("ascii", errors="ignore").decode())
  40.                         color_list = [];
  41.                         color_list = driver.find_elements_by_class_name("add-to-bag__color");
  42.                         try:
  43.                                 clearance_list2 = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[3]/div[2]/button");
  44.                                 clearance_list = 1
  45.                         except:
  46.                                 clearance_list2 = 0
  47.                                 try:
  48.                                         clearance_list = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[2]/button");
  49.                                 except:
  50.                                         clearance_list = 0
  51.                         if len(color_list) == 1 and clearance_list == 0 and clearance_list2 == 0:
  52.                             color=(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]
  53.                             color=" ".join(color)
  54.                             print str(color)
  55.                             color_url = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div/div/button/img").get_attribute("src")
  56.                             print str(color_url)
  57.                         elif len(color_list) > 1 and clearance_list == 0 and clearance_list2 == 0:
  58.                             color = []
  59.                             color_url = []
  60.                             for x in range(1, len(color_list)+1):
  61.                                
  62.                                 try:
  63.                                     button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div/div[" + str(x) + "]/button")
  64.                                     button.click()
  65.                                     if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  66.                                         button.click();
  67.                                     color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  68.                                     color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div/div[" + str(x) + "]/button/img").get_attribute("src"))                                  
  69.                                 except:
  70.  
  71.                                     try:
  72.                                         button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div/div[" + str(x) + "]/button")
  73.                                         button.send_keys(Keys.SPACE)  #using another method to click
  74.                                         if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  75.                                             button.send_keys(Keys.SPACE);
  76.                                         color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  77.                                         color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div/div[" + str(x) + "]/button/img").get_attribute("src"))    
  78.                                     except:
  79.                                         "The button could not be clicked.\n"
  80.                             if color:
  81.                                 try:
  82.                                     button.click();                                
  83.                                 except:
  84.                                     try:
  85.                                         button.send_keys(Keys.SPACE)  #using another method to click
  86.                                     except:
  87.                                         "The button could not be clicked (deselect operation).\n"
  88.                                 color=", ".join(color)
  89.                                 print str(color)
  90.                                 color_url=", ".join(color_url)
  91.                                 print str(color_url)
  92.                         elif clearance_list != 0 and clearance_list2 == 0:
  93.                                 color = []
  94.                                 color_url = []
  95.                                 color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div");
  96.                                 for x in range(2, len(color_list)+1):
  97.                                         button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div[" + str(x) + "]/button")
  98.                                         try:
  99.                                             button.click();
  100.                                             if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  101.                                                 button.click();
  102.                                             color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  103.                                             color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div[" + str(x) + "]/button/img").get_attribute("src"))                                  
  104.                                         except:
  105.                                             try:
  106.                                                 button.send_keys(Keys.SPACE)  #using another method to click
  107.                                                 if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  108.                                                     button.send_keys(Keys.SPACE);
  109.                                                 color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  110.                                                 color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div[" + str(x) + "]/button/img").get_attribute("src"))    
  111.                                             except:
  112.                                                 "The button could not be clicked.\n"
  113.                                 if color:
  114.                                         color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div");
  115.                                         #print len(color_list)
  116.                                         for x in range(2, len(color_list)+1):
  117.                                                 button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[" + str(x) + "]/button")
  118.                                                 try:
  119.                                                     button.click();
  120.                                                     if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  121.                                                         button.click();
  122.                                                     color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  123.                                                     color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[" + str(x) + "]/button/img").get_attribute("src"))              
  124.                                                 except:
  125.                                                         try:
  126.                                                             button.send_keys(Keys.SPACE)  #using another method to click
  127.                                                         except:
  128.                                                             if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  129.                                                                 button.click();
  130.                                                             color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  131.                                                             try:
  132.                                                                 color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[" + str(x) + "]button/img").get_attribute("src"))
  133.                                                             except InvalidSelectorException:
  134.                                                                 color_url.append("blank color url")
  135.                                                             except NoSuchElementException:
  136.                                                                 color_url.append("blank color url")
  137.                                                             except SyntaxError:
  138.                                                                 color_url.append("blank color url")
  139.  
  140.  
  141.  
  142.                                                 try:        
  143.                                                     button.click();                                
  144.                                                 except:
  145.                                                     try:
  146.                                                         button.send_keys(Keys.SPACE)  #using another method to click
  147.                                                     except:
  148.                                                         "The button could not be clicked (deselect operation).\n"
  149.                                         color=", ".join(color)
  150.                                         print str(color)
  151.                                         color_url=", ".join(color_url)
  152.                                         print str(color_url)
  153.                         elif clearance_list2 != 0:
  154.                                 color = []
  155.                                 color_url = []
  156.                                 color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div");
  157.                                 print len(color_list)
  158.                                 for x in range(2, len(color_list)+1):
  159.                                         button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div[" + str(x) + "]/button")
  160.                                         try:
  161.                                             button.click();
  162.                                             if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  163.                                                 button.click();
  164.                                             color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  165.                                             color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div[" + str(x) + "]/button/img").get_attribute("src"))                                  
  166.                                         except:
  167.                                             try:
  168.                                                 button.send_keys(Keys.SPACE)  #using another method to click
  169.                                                 if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  170.                                                     button.send_keys(Keys.SPACE);
  171.                                                 color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  172.                                                 color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div[" + str(x) + "]/button/img").get_attribute("src"))    
  173.                                             except:
  174.                                                 "The button could not be clicked.\n"
  175.                                 if color:
  176.                                         color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div");
  177.                                         for x in range(2, len(color_list)+1):
  178.                                                 button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[" + str(x) + "]/button")
  179.                                                 try:
  180.                                                     button.click();
  181.                                                     if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  182.                                                         button.click();
  183.                                                     color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  184.                                                     color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[" + str(x) + "]/button/img").get_attribute("src"))              
  185.                                                 except:
  186.                                                         try:
  187.                                                             button.send_keys(Keys.SPACE)  #using another method to click
  188.                                                         except:
  189.                                                             if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  190.                                                                 button.click();
  191.                                                             color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  192.                                                             color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div[" + str(x) + "]button/img").get_attribute("src"))
  193.                                         color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[3]/div");
  194.                                         for x in range(2, len(color_list)+1):
  195.                                                 button = driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[3]/div[" + str(x) + "]/button")
  196.                                                 try:
  197.                                                     button.click();
  198.                                                     if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  199.                                                         button.click();
  200.                                                     color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  201.                                                     color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[3]/div[" + str(x) + "]/button/img").get_attribute("src"))              
  202.                                                 except:
  203.                                                         try:
  204.                                                             button.send_keys(Keys.SPACE)  #using another method to click
  205.                                                         except:
  206.                                                             if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
  207.                                                                 button.click();
  208.                                                             color.append(" ".join((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]))
  209.                                                             try:
  210.                                                                 color_url.append(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[3]/div[" + str(x) + "]button/img").get_attribute("src"))
  211.                                                             except:
  212.                                                                 color_url.append("failed to fetch color url")
  213.  
  214.  
  215.                                                 try:
  216.                                                     button.click();                                
  217.                                                 except:
  218.                                                     try:
  219.                                                         button.send_keys(Keys.SPACE)  #using another method to click
  220.                                                     except:
  221.                                                         "The button could not be clicked (deselect operation).\n"
  222.                                         color=", ".join(color)
  223.                                         print str(color)
  224.                                         color_url=", ".join(color_url)
  225.                                         print str(color_url)  
  226.                         if len(color_list) == 0:
  227.                                 color = "No colors available."
  228.                                 print str(color)
  229.                                 color_url = " "
  230.                                 print str(color_url)
  231.                                 sizes = "The item is out of stock"
  232.                         else:
  233.                                 try:
  234.                                        sizes=", ".join((driver.find_element_by_xpath('//*[@id="pdp-size"]').text).split('\n')[1:])
  235.                                 except:
  236.                                        sizes = "The item is out of stock"
  237.                         print str(sizes)
  238.                         f2.write(str(line.split("\n")[0]) + "\t" + str(item_no) + "\t" + str(description.encode("ascii", errors="ignore").decode()) + "\t" + str(color) + "\t" + str(color_url) + "\t" + str(sizes) + '\n')
  239.                         time.sleep(SLEEPTIME)
  240.  
  241. f1.close()
  242. f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement