-
-
Save anonymous/00789e9cc3c13fc6477c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env/python | |
# -*- coding: utf-8 -*- | |
from selenium.webdriver.common.action_chains import ActionChains | |
import time | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.wait import WebDriverWait | |
from selenium import webdriver | |
from selenium.common.exceptions import NoSuchElementException | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import Select | |
def get_browser(): | |
return webdriver.Chrome('chromedriver.exe') | |
#def get_browser(): | |
# return webdriver.PhantomJS("phantomjs.exe") | |
main_page_url = "http://slampa.pl/" | |
basic_url = 'http://slampa.pl/ogloszenia-kobiet.html' | |
def get_city_list(url) : | |
AGE_ACCEPT_BUTTON_XPATH = ".//*[@id='columns']/div/div[2]/section/div/div/div/div/div/div[2]/div[2]/a[2]" | |
COMBOBOX_XPATH = ".//*[@id='select_city']/li/form/div/button" | |
COMBOBOX_OPTION_XPATH = ".//*[@id='select_city']/li/form/div/div/ul/li[%s]/a/span[1]" | |
CHOOSE_BUTTON_XPATH = ".//*[@id='select_city']/li/form/button" | |
pages = [] | |
try: | |
browser = get_browser() | |
wait = WebDriverWait(browser, 100) | |
browser.get(main_page_url) | |
time.sleep(2) | |
button_age_accept = browser.find_element_by_xpath(AGE_ACCEPT_BUTTON_XPATH) | |
button_age_accept.click() | |
time.sleep(10) | |
browser.get(url) | |
i = 2 | |
while(True) : | |
try : | |
button_combobox = browser.find_element_by_xpath(COMBOBOX_XPATH) | |
button_combobox.click() | |
time.sleep(5) | |
element_xpath = COMBOBOX_OPTION_XPATH % i | |
option_in_combobox = browser.find_element_by_xpath(element_xpath) | |
browser.execute_script("arguments[0].scrollIntoView();", option_in_combobox) | |
option_in_combobox.click() | |
button_choose = browser.find_element_by_xpath(CHOOSE_BUTTON_XPATH) | |
button_choose.click() | |
time.sleep(5) | |
pages.append(browser.current_url) | |
i += 1 | |
except Exception, e: | |
print e | |
break | |
browser.close() | |
return pages | |
except Exception, e: | |
info = 'Generic exception\n' | |
print e | |
return [] | |
pages = get_city_list(basic_url) | |
for p in pages : | |
with open('links.txt', 'a') as the_file: | |
the_file.write(p) | |
the_file.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment