Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support.ui import Select
- import time
- from random import randint
- from selenium.webdriver.common.keys import Keys
- from selenium.common.exceptions import NoSuchElementException
- from selenium.common.exceptions import InvalidSelectorException
- SLEEPTIME=1
- if __name__ == '__main__':
- #if below doesnt work
- driver=webdriver.Chrome()
- driver.maximize_window()
- raw_input("Should I proceed? ") # add VPN extension before it loads a page in web browser
- fileName='output.txt'
- with open("urls2.txt", "r") as f1:
- with open(fileName,'a') as f2:
- f2.write("Page URL" + "\t" + "Item No." + "\t" + "Description" + "\t" + "Color Name" + "\t" + "Color URL" + "\t" + "Medium Sizes" + '\n')
- for line in f1:
- driver.get(line)
- driver.implicitly_wait(4) #give an implicit wait so that the page gets loaded completely
- print str(line.split("\n")[0])
- try:
- 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")
- except NoSuchElementException:
- item_no="could not find item no"
- print str(item_no)
- description=driver.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/content-expander/div/div[2]/ng-transclude").get_attribute('innerHTML')
- print str(description.encode("ascii", errors="ignore").decode())
- color_list = [];
- color_list = driver.find_elements_by_class_name("add-to-bag__color");
- try:
- 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");
- clearance_list = 1
- except:
- clearance_list2 = 0
- try:
- 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");
- except:
- clearance_list = 0
- if len(color_list) == 1 and clearance_list == 0 and clearance_list2 == 0:
- color=(driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")[1:]
- color=" ".join(color)
- print str(color)
- 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")
- print str(color_url)
- elif len(color_list) > 1 and clearance_list == 0 and clearance_list2 == 0:
- color = []
- color_url = []
- for x in range(1, len(color_list)+1):
- try:
- 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")
- button.click()
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- except:
- try:
- 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")
- button.send_keys(Keys.SPACE) #using another method to click
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.send_keys(Keys.SPACE);
- 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:]))
- 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"))
- except:
- "The button could not be clicked.\n"
- if color:
- try:
- button.click();
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- except:
- "The button could not be clicked (deselect operation).\n"
- color=", ".join(color)
- print str(color)
- color_url=", ".join(color_url)
- print str(color_url)
- elif clearance_list != 0 and clearance_list2 == 0:
- color = []
- color_url = []
- color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div");
- for x in range(2, len(color_list)+1):
- 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")
- try:
- button.click();
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.send_keys(Keys.SPACE);
- 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:]))
- 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"))
- except:
- "The button could not be clicked.\n"
- if color:
- color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div");
- #print len(color_list)
- for x in range(2, len(color_list)+1):
- 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")
- try:
- button.click();
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- except:
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- try:
- 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"))
- except InvalidSelectorException:
- color_url.append("blank color url")
- except NoSuchElementException:
- color_url.append("blank color url")
- except SyntaxError:
- color_url.append("blank color url")
- try:
- button.click();
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- except:
- "The button could not be clicked (deselect operation).\n"
- color=", ".join(color)
- print str(color)
- color_url=", ".join(color_url)
- print str(color_url)
- elif clearance_list2 != 0:
- color = []
- color_url = []
- color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[1]/div");
- print len(color_list)
- for x in range(2, len(color_list)+1):
- 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")
- try:
- button.click();
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.send_keys(Keys.SPACE);
- 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:]))
- 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"))
- except:
- "The button could not be clicked.\n"
- if color:
- color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[2]/div");
- for x in range(2, len(color_list)+1):
- 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")
- try:
- button.click();
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- except:
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- color_list = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div/div[3]/div");
- for x in range(2, len(color_list)+1):
- 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")
- try:
- button.click();
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- 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"))
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- except:
- if len((driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/span").text).split(" ")) == 1:
- button.click();
- 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:]))
- try:
- 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"))
- except:
- color_url.append("failed to fetch color url")
- try:
- button.click();
- except:
- try:
- button.send_keys(Keys.SPACE) #using another method to click
- except:
- "The button could not be clicked (deselect operation).\n"
- color=", ".join(color)
- print str(color)
- color_url=", ".join(color_url)
- print str(color_url)
- if len(color_list) == 0:
- color = "No colors available."
- print str(color)
- color_url = " "
- print str(color_url)
- sizes = "The item is out of stock"
- else:
- try:
- sizes=", ".join((driver.find_element_by_xpath('//*[@id="pdp-size"]').text).split('\n')[1:])
- except:
- sizes = "The item is out of stock"
- print str(sizes)
- 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')
- time.sleep(SLEEPTIME)
- f1.close()
- f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement