from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import random
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as b
import time
from selenium.common.exceptions import ElementClickInterceptedException
import winsound


browser = webdriver.Firefox(executable_path="C:/Users/unknown/Desktop/projects/poshmark/geckodriver")
wait = WebDriverWait(browser, 30)
browser.get('https://poshmark.com/login')
usename = browser.find_element_by_id('login_form_username_email')
usename.send_keys('SpeanutButter')
psw = browser.find_element_by_id('login_form_password')
psw.send_keys('***REMOVED***')
iframe = 'default value'

def reCaptcha_finder(): # Still not working. Works on the first recaptcha popup but not second one for some reason
    global iframe
    time.sleep(4)
    iframes = browser.find_elements_by_tag_name('iframe')
    for frame in iframes:
        if not iframe in iframes:
            browser.switch_to.frame(frame)
            try:
                browser.find_element_by_xpath('//label[text()="I\'m not a robot"]')
                winsound.Beep(540, 1000)
                iframe = frame
                input('Complete reCaptcha then press "enter" to continue')
                browser.switch_to.default_content()
                break
            except NoSuchElementException:
                browser.switch_to.default_content()

psw.submit()

reCaptcha_finder()
# have to find different element for log in after recaptcha is completed. Do manually for now

wait.until(EC.presence_of_element_located((By.XPATH, "//title[text()='Feed - Poshmark']")))
browser.get('https://poshmark.com/closet/speanutbutter?department=Women&sort_by=price_desc')
input('press "enter" to continue')

html = browser.page_source
soup = b(html, "lxml")
elm = browser.find_element_by_tag_name('html')
while not soup.find('span', text="Not for Sale"):
    elm.send_keys(Keys.END)
    html = browser.page_source
    soup = b(html)
list_titles = soup.find_all('a', {'class': 'title'})
active_listings = soup.find_all('i', {'class': "icon share-gray"})
print(len(active_listings))
container = browser.find_elements_by_xpath("//div[@id='tiles-con']/div")
i = -1
share_to = input('share to followers (F) or share to party (P)?: ')
for divs in container:
    i += 1
    wait.until(EC.presence_of_element_located((By.XPATH, ".//i[@class = 'icon share-gray']")))
    try:
        if divs.find_element_by_xpath(".//i[@class = 'icon inventory-tag not-for-sale-tag']"):
            pass
    except NoSuchElementException:
        try:
            if divs.find_element_by_xpath(".//i[@class = 'icon inventory-tag sold-tag']"):
                pass
        except NoSuchElementException:
            share = divs.find_element_by_xpath(".//i[@class = 'icon share-gray']")
            time.sleep(random.uniform(.6, 1.2))
            try:
                share.click()
                # reCaptcha_finder() # Might only need reCaptcha_finder() here and not afterwards or the other way around. Pay attention to where the recaptcha occurs
                if share_to == 'F':
                    wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='To My Followers']")))
                    share = browser.find_element_by_xpath("//span[text()='To My Followers']")
                    time.sleep(random.uniform(.6, 1.2))
                    share.click()
                    reCaptcha_finder()
                    print(i)
                    title = list_titles[i].get_text()
                    print(title)
                if share_to == 'P':
                    wait.until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'Happening Now')]")))
                    share = browser.find_element_by_xpath("//*[contains(text(), 'Happening Now')]")
                    time.sleep(random.uniform(.6, 1.2))
                    share.click()
                    reCaptcha_finder()
                    print(i)
                    title = list_titles[i].get_text()
                    print(title)
            except ElementClickInterceptedException:
                pass

# If poshmark lets you browser.get any page then you should skip the pagination loading to load all the pages and then just go through each page and share that way. It wouldn't be such a time consuming process