"""
#pip install selenium
#pip install BeautifulSoup
#pip install pyvirtualdisplay
#sudo apt-get install xvfb
"""
"""
Driver
=======
>>> dir(d)
['NATIVE_EVENTS_ALLOWED', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_is_remote', '_unwrap_value', '_wrap_value', 'add_cookie', 'application_cache', 'back', 'binary', 'capabilities', 'close', 'command_executor', 'create_web_element', 'current_url', 'current_window_handle', 'delete_all_cookies', 'delete_cookie', 'desired_capabilities', 'error_handler', 'execute', 'execute_async_script', 'execute_script', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'firefox_profile', 'forward', 'get', 'get_cookie', 'get_cookies', 'get_screenshot_as_base64', 'get_screenshot_as_file', 'get_window_position', 'get_window_size', 'implicitly_wait', 'is_online', 'maximize_window', 'name', 'orientation', 'page_source', 'profile', 'quit', 'refresh', 'save_screenshot', 'session_id', 'set_page_load_timeout', 'set_script_timeout', 'set_window_position', 'set_window_size', 'start_client', 'start_session', 'stop_client', 'switch_to_active_element', 'switch_to_alert', 'switch_to_default_content', 'switch_to_frame', 'switch_to_window', 'title', 'window_handles']
>>>
Element
=======
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_execute', '_id', '_parent', '_upload', 'clear', 'click', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'get_attribute', 'id', 'is_displayed', 'is_enabled', 'is_selected', 'location', 'location_once_scrolled_into_view', 'parent', 'send_keys', 'size', 'submit', 'tag_name', 'text', 'value_of_css_property']
"""
import selenium
from selenium import webdriver
from selenium import selenium as sel
import time
from selenium.webdriver.support.ui import WebDriverWait
import BeautifulSoup
from pyvirtualdisplay import Display
import sys
from PIL import Image
from urllib import urlretrieve
import logging
LOGGER = logging.getLogger('selenium')
hdlr = logging.FileHandler('./selenium.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
LOGGER.addHandler(hdlr)
LOGGER.setLevel(logging.INFO)
class MeetMe:
"""
"""
TARGET_URL = "http://www.blabla.com"
DUMP_FILE = "response.html"
CAPTCHA_FILE_NAME = "captcha.jpeg"
def __init__(self):
"""
"""
self.display = Display(visible=0, size=(1200,1600))
self.display.start()
self.url = self.TARGET_URL
self.driver = webdriver.Firefox()
self.retry = 0
LOGGER.info("\n-------------start---------------\n")
def __del__(self):
"""
"""
print "====closing browser=="
self.close_browser()
def open_url(self, url):
"""
"""
self.driver.get(url)
def find_element_by_name(self, name):
"""
"""
return self.driver.find_element_by_name(name)
def find_element_by_id(self, id):
"""
"""
return self.driver.find_element_by_id(id)
def close_browser(self):
"""
"""
self.driver.close()
def dump_response_to_file(self, filename=None):
"""
"""
if not filename:
filename = self.DUMP_FILE
html = self.driver.page_source
soup_res = BeautifulSoup.BeautifulSoup(html)
with open(filename, 'w') as fp:
fp.write(str(soup_res))
def execute_script(self, script):
"""
self.driver.execute_script("document.getElementsByName('body')[0].setAttribute('type', 'text');")
"""
self.driver.execute_script(script)
def save_screenshot(self, filename):
"""
"""
self.driver.save_screenshot(filename)
def select_checkbox(self, element, select=True):
"""
"""
if select:
if not element.is_selected():
element.click()
else:
if element.is_selected():
element.click()
def get_file(self, link, filename="test.tif"):
##Download and save file with name filename
urlretrieve(link, filename)
def show_image(self, image_name):
"""
"""
im = Image.open(image_name)
im.show()
"""
signup_frm_elements_id = {
#"quicksignupFrm":"quicksignupFrm",##
"""
### CAPCHA ###
"""
recaptcha_image_element = me.find_element_by_id("recaptcha_image")
if recaptcha_image_element:
##Get child tag "img" of tag "recaptcha_image"
recaptcha_img_tag = recaptcha_image_element.find_element_by_tag_name("img")
if recaptcha_img_tag:
##Get url of captcha image
recaptcha_img_src = recaptcha_img_tag.get_attribute("src")
print "===recaptcha_img_src===", recaptcha_img_src
me.get_file(recaptcha_img_src, filename=me.CAPTCHA_FILE_NAME)
me.show_image(me.CAPTCHA_FILE_NAME)
captcha_chars = raw_input("Enter Captcha:")
recaptcha_text_field = me.find_element_by_id("recaptcha_response_field")
recaptcha_text_field.send_keys(captcha_chars)
me.save_screenshot("k3")
me.dump_response_to_file("signuotest1.html")
me.save_screenshot("k4")
#pip install selenium
#pip install BeautifulSoup
#pip install pyvirtualdisplay
#sudo apt-get install xvfb
"""
"""
Driver
=======
>>> dir(d)
['NATIVE_EVENTS_ALLOWED', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_is_remote', '_unwrap_value', '_wrap_value', 'add_cookie', 'application_cache', 'back', 'binary', 'capabilities', 'close', 'command_executor', 'create_web_element', 'current_url', 'current_window_handle', 'delete_all_cookies', 'delete_cookie', 'desired_capabilities', 'error_handler', 'execute', 'execute_async_script', 'execute_script', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'firefox_profile', 'forward', 'get', 'get_cookie', 'get_cookies', 'get_screenshot_as_base64', 'get_screenshot_as_file', 'get_window_position', 'get_window_size', 'implicitly_wait', 'is_online', 'maximize_window', 'name', 'orientation', 'page_source', 'profile', 'quit', 'refresh', 'save_screenshot', 'session_id', 'set_page_load_timeout', 'set_script_timeout', 'set_window_position', 'set_window_size', 'start_client', 'start_session', 'stop_client', 'switch_to_active_element', 'switch_to_alert', 'switch_to_default_content', 'switch_to_frame', 'switch_to_window', 'title', 'window_handles']
>>>
Element
=======
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_execute', '_id', '_parent', '_upload', 'clear', 'click', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'get_attribute', 'id', 'is_displayed', 'is_enabled', 'is_selected', 'location', 'location_once_scrolled_into_view', 'parent', 'send_keys', 'size', 'submit', 'tag_name', 'text', 'value_of_css_property']
"""
import selenium
from selenium import webdriver
from selenium import selenium as sel
import time
from selenium.webdriver.support.ui import WebDriverWait
import BeautifulSoup
from pyvirtualdisplay import Display
import sys
from PIL import Image
from urllib import urlretrieve
import logging
LOGGER = logging.getLogger('selenium')
hdlr = logging.FileHandler('./selenium.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
LOGGER.addHandler(hdlr)
LOGGER.setLevel(logging.INFO)
class MeetMe:
"""
"""
TARGET_URL = "http://www.blabla.com"
DUMP_FILE = "response.html"
CAPTCHA_FILE_NAME = "captcha.jpeg"
def __init__(self):
"""
"""
self.display = Display(visible=0, size=(1200,1600))
self.display.start()
self.url = self.TARGET_URL
self.driver = webdriver.Firefox()
self.retry = 0
LOGGER.info("\n-------------start---------------\n")
def __del__(self):
"""
"""
print "====closing browser=="
self.close_browser()
def open_url(self, url):
"""
"""
self.driver.get(url)
def find_element_by_name(self, name):
"""
"""
return self.driver.find_element_by_name(name)
def find_element_by_id(self, id):
"""
"""
return self.driver.find_element_by_id(id)
def close_browser(self):
"""
"""
self.driver.close()
def dump_response_to_file(self, filename=None):
"""
"""
if not filename:
filename = self.DUMP_FILE
html = self.driver.page_source
soup_res = BeautifulSoup.BeautifulSoup(html)
with open(filename, 'w') as fp:
fp.write(str(soup_res))
def execute_script(self, script):
"""
self.driver.execute_script("document.getElementsByName('body')[0].setAttribute('type', 'text');")
"""
self.driver.execute_script(script)
def save_screenshot(self, filename):
"""
"""
self.driver.save_screenshot(filename)
def select_checkbox(self, element, select=True):
"""
"""
if select:
if not element.is_selected():
element.click()
else:
if element.is_selected():
element.click()
def get_file(self, link, filename="test.tif"):
##Download and save file with name filename
urlretrieve(link, filename)
def show_image(self, image_name):
"""
"""
im = Image.open(image_name)
im.show()
"""
signup_frm_elements_id = {
#"quicksignupFrm":"quicksignupFrm",##
"""
### CAPCHA ###
"""
recaptcha_image_element = me.find_element_by_id("recaptcha_image")
if recaptcha_image_element:
##Get child tag "img" of tag "recaptcha_image"
recaptcha_img_tag = recaptcha_image_element.find_element_by_tag_name("img")
if recaptcha_img_tag:
##Get url of captcha image
recaptcha_img_src = recaptcha_img_tag.get_attribute("src")
print "===recaptcha_img_src===", recaptcha_img_src
me.get_file(recaptcha_img_src, filename=me.CAPTCHA_FILE_NAME)
me.show_image(me.CAPTCHA_FILE_NAME)
captcha_chars = raw_input("Enter Captcha:")
recaptcha_text_field = me.find_element_by_id("recaptcha_response_field")
recaptcha_text_field.send_keys(captcha_chars)
me.save_screenshot("k3")
me.dump_response_to_file("signuotest1.html")
me.save_screenshot("k4")
No comments:
Post a Comment