class Bumble(ImageMixin): matched_users = deque() users = deque() def __init__(self): self.driver = Edge(executable_path=os.environ.get('EDGE_DRIVER')) self.driver.get('https://bumble.com') def login(self, username, password): time.sleep(5) try: login_button = self.driver.find_element_by_xpath( '//*[@id="page"]/div/div/div[1]/div/div[2]/div/div/div/div[2]/div[1]/div/div[2]/a' ) except: print('Could not connect.') return False else: login_button.click() time.sleep(10) # Facebook login facebook = self.driver.find_element_by_xpath( '//*[@id="main"]/div/div[1]/div[2]/main/div/div[2]/form/div[1]/div/div[2]/div' ) facebook.click() time.sleep(5) base_window = self.driver.window_handles[0] self.driver.switch_to.window(self.driver.window_handles[1]) username_field = self.driver.find_element_by_xpath('//*[@id="email"]') password_field = self.driver.find_element_by_xpath('//*[@id="pass"]') username_field.send_keys(username) password_field.send_keys(password) login_button = self.driver.find_element_by_xpath('//*[@id="u_0_0"]') login_button.click() self.driver.switch_to.window(base_window) time.sleep(5) return True def like(self): self.user_profile() self.driver.find_element_by_tag_name('body').send_keys( Keys.ARROW_RIGHT) time.sleep(3) return True def dislike(self): self.driver.find_element_by_tag_name('body').send_keys(Keys.ARROW_LEFT) time.sleep(3) return True def auto_swipe(self, n=10): for _ in range(1, n): self.like() return self.matched_users def user_profile(self): name_and_age = self.driver.find_element_by_xpath( '//*[@id="main"]/div/div[1]/main/div[2]/div/div/span/div[1]/article/div[1]/div[1]/article/div[2]/section/header/h1' ) image = self.driver.find_element_by_xpath( '//*[@id="main"]/div/div[1]/main/div[2]/div/div/span/div[1]/article/div[1]/div[1]/article/div[1]/figure/picture/img' ) user = name_and_age.text, image.get_attribute('src') if user: self.users.append(user) time.sleep(1) return True time.sleep(1) return False
class SeleniumRuntime: """ This class works like a singleton containing a single instance of the browser environment Attributes: logger: logger instance gathered from logging module, acts like a singleton """ def __init__(self): self.logger = logging.getLogger(LOGGER_INSTANCE) if TARGET_BROWSER == 'chrome': self.browser = Chrome() elif TARGET_BROWSER == 'firefox': self.browser = Firefox() elif TARGET_BROWSER == 'edge': self.browser = Edge() def go_to_page(self, url): self.browser.get(url) def submit_form(self): form = self.browser.find_element_by_tag_name('form') form.submit() def fill_form(self, table): for field, value in table.items(): element = self.browser.find_element_by_id(field) element.clear() element.send_keys(value) def fill_selects(self, table): for field_name, field_value in table.items(): self.wait_for_element(field_value, By.XPATH).click() def click(self, value, by=By.ID): element = self.browser.find_element(by, value) element.click() def get_element(self, value, by=By.ID): return self.browser.find_element(by, value) def get_elements(self, value, by=By.ID): return self.browser.find_elements(by, value) def assert_presence(self, value, by=By.ID): try: self.browser.find_element(by, value) return True except NoSuchElementException: return False def back(self): self.browser.back() def forward(self): self.browser.forward() def refresh(self): self.browser.refresh() def current_title(self): return self.browser.title def current_url(self): return self.browser.current_url def wait_for_element(self, value, by=By.ID, timeout=30): return WebDriverWait(self.browser, timeout).until( expected_conditions.presence_of_element_located((by, value))) def wait_for_redirect(self, target_url, timeout=30): return WebDriverWait(self.browser, timeout).until( expected_conditions.url_to_be(target_url)) @staticmethod def assert_class(element, class_name): class_attr = element.get_attribute('class') return class_attr.find(class_name) >= 0 @staticmethod def assert_attribute(element, attribute_name, attribute_value): attr_value = element.get_attribute(attribute_name) return attr_value.find(attribute_value) >= 0
from selenium.webdriver import Edge import random driver = Edge() driver.get("https://www.igame.com/eye-test/") driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) score = 0 if score > 0 and score < 9: co_wybrac = random.randint(0, 1) if co_wybrac == 0 el = driver.find_element_by_class_name("thechosenone") el.click()
def selenium_parser(url, xpath, url_suffix=None): """ Selenium pages by retrieving the links to each team roster in the menu dropdown """ # driver = Edge(executable_path='C:\\Users\\Pende\\Documents\\edge_driver\\msedgedriver.exe') driver = Edge(executable_path=os.environ.get('EDGE_DRIVER')) driver.get(url) time.sleep(2) nav_ul = driver.find_element_by_xpath(xpath) links = nav_ul.find_elements_by_tag_name('a') list_of_urls = [] for index, link in enumerate(links): country = f'NOCOUNTRY{index}' href = link.get_attribute('href') if '/en/volleyball' in href: is_match = re.search(r'\/teams\/(\w+)\-(.*)', href) if is_match: country = is_match.group(1).upper() if 'Teams.asp' in href \ or 'Team=' in href \ or '/Teams/' in href: is_match = re.search(r'Team\=(.*)', href) if is_match: country = is_match.group(1) if '/competions/teams/' in href: is_match = re.search(r'teams\/(\w+)(?=\-|\s+)', href) if is_match: country = is_match.group(1).capitalize() if url_suffix: href = f'{href}/{url_suffix}' list_of_urls.append((href, country)) list_of_urls = list(set(list_of_urls)) async def writer(output_path, html): with open(output_path, 'w') as f: try: f.write(html) except: return False print(f'Writing file to {output_path}') asyncio.sleep(1) async def main(output_path, html): return await writer(output_path, html) for list_of_url in list_of_urls: driver.get(list_of_url[0]) # We just retrieve the body for # simplification instead of taking # the full HTML tag body = driver.find_element_by_tag_name('body') output_path = os.path.join(PAGES_PATH, 'temp', f'{list_of_url[1]}.html') html = body.get_attribute('innerHTML') asyncio.run(main(output_path, html))
class Tinder: matched_users = [] def __init__(self): self.driver = Edge(executable_path=os.environ.get('EDGE_DRIVER')) self.driver.get('https://tinder.com') def login(self, username, password): WebDriverWait(self.driver, 6000).until(expected_conditions \ .element_to_be_clickable((By.XPATH, '//*[@id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button'))) try: facebook = self.driver.find_element_by_xpath( '//*[@id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button' ) except: print('Could not connect.') return False else: facebook.click() base_window = self.driver.window_handles[0] self.driver.switch_to.window(self.driver.window_handles[1]) username_field = self.driver.find_element_by_xpath('//*[@id="email"]') password_field = self.driver.find_element_by_xpath('//*[@id="pass"]') username_field.send_keys(username) password_field.send_keys(password) login_button = self.driver.find_element_by_xpath('//*[@id="u_0_0"]') login_button.click() self.driver.switch_to.window(base_window) time.sleep(10) localization_button = self.driver.find_element_by_xpath( '//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]') localization_button.click() time.sleep(10) try: accept_to_receive_messages = self.driver.find_element_by_xpath( '//*[@id="modal-manager"]/div/div/div/div/div[3]/button[2]') except: print( 'Could not find message button or message modal did not pop up.' ) return False else: accept_to_receive_messages.click() def like(self): # like_button = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/div[4]/button') # self.button_click(like_button) # Test for the # random pop screen # popup = self.close_random_popup() # if popup: # popup.click() # time.sleep(1) self.driver.find_element_by_tag_name('body').send_keys( Keys.ARROW_RIGHT) time.sleep(3) try: # If we have a match, we have to # manage the card in question by # clicking on the link match_card_link = self.driver.find_element_by_xpath( '//*[@id="modal-manager-canvas"]/div/div/div[1]/div/div[3]/a') except: # There is no match, then continue # swiping until we find one return False else: match_card_link.click() self.matched_users.append(1) return True def dislike(self): dislike_button = self.driver.find_element_by_xpath( '//*[@id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/div[2]/button' ) self.button_click(dislike_button) def auto_swipe(self, n=10): for _ in range(1, n): self.like() time.sleep(3) return self.matched_users @staticmethod def button_click(button): try: button.click() except: return False else: return True def close_random_popup(self): WebDriverWait(self.driver, 2000).until( expected_conditions.visibility_of( (By.XPATH, '//*[@id="modal-manager"]'))) try: # Deals with the pop up that asks us # to add tinder to the homescreen home_screen_button = self.driver.find_element_by_xpath( '//*[@id="modal-manager"]/div/div/div[2]/button[2]') except: return False else: return home_screen_button