Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Added Captcha from DreckSoft plus changes in delivery options #36

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 61 additions & 17 deletions kleinanzeigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,28 @@ def login(config):
input_email = config['glob_username']
input_pw = config['glob_password']
log.info("Login with account email: " + input_email)
driver.get('https://www.ebay-kleinanzeigen.de/m-einloggen.html')


driver.get('https://www.ebay-kleinanzeigen.de')

# wait for the 'accept cookie' banner to appear
WebDriverWait(driver, 6).until(EC.element_to_be_clickable((By.ID, 'gdpr-banner-accept'))).click()


fake_wait(2000)

driver.get('https://www.ebay-kleinanzeigen.de/m-einloggen.html')

fake_wait(2000)

text_area = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.ID, 'login-email')))
text_area.send_keys(input_email)
fake_wait(200)

has_captcha = login_has_captcha(driver)
if has_captcha:
log.info("\t*** Manual captcha input needed! ***")
log.info("\tFill out captcha but DON'T submit. After that press Enter here to continue ...")
wait_key()

text_area = driver.find_element_by_id('login-password')
text_area.send_keys(input_pw)
fake_wait(200)
Expand Down Expand Up @@ -95,26 +108,38 @@ def delete_ad(driver, ad):

if "id" in ad:
try:
ad_id_elem = driver.find_element_by_xpath("//a[@data-adid='%s']" % ad["id"])
ad_id_elem = driver.find_element_by_xpath("//li[@data-adid='%s']" % ad["id"])
except NoSuchElementException:
log.info("\tNot found by ID")
try:
next_page = driver.find_element_by_class_name("Pagination--next")
next_page.click()
fake_wait()
ad_id_elem = driver.find_element_by_xpath("//li[@data-adid='%s']" % ad["id"])
except NoSuchElementException:
log.info("\tNot found by ID on second page")

if ad_id_elem is None:
try:
ad_id_elem = driver.find_element_by_xpath("//a[contains(text(), '%s')]/../../../../.." % ad["title"])
ad_id_elem = driver.find_element_by_xpath("//article[.//a[contains(text(), '%s')]]" % ad["title"])
except NoSuchElementException:
log.info("\tNot found by title")

if ad_id_elem is not None:
try:
btn_del = ad_id_elem.find_elements_by_class_name("managead-listitem-action-delete")[1]
btn_del = ad_id_elem.find_element_by_class_name("managead-listitem-action-delete")
btn_del.click()

fake_wait()

btn_confirm_del = driver.find_element_by_id("modal-bulk-delete-ad-sbmt")

toogle_delete_reason = driver.find_element_by_id("DeleteWithoutReason")
toogle_delete_reason.click()

fake_wait()

btn_confirm_del = driver.find_element_by_id("sold-celebration-sbmt")
btn_confirm_del.click()

log.info("\tAd deleted")
fake_wait(randint(2000, 3000))
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
Expand Down Expand Up @@ -152,6 +177,19 @@ def wait_key():

return result

def login_has_captcha(driver):
has_captcha = False

try:
captcha_field = driver.find_element_by_xpath('//*[@id="login-recaptcha"]')
if captcha_field:
has_captcha = True
except NoSuchElementException:
pass

log.info(f"Captcha: {has_captcha}")

return has_captcha

def post_ad_has_captcha(driver):
has_captcha = False
Expand Down Expand Up @@ -296,7 +334,13 @@ def post_ad(driver, ad, interactive):
text_area = driver.find_element_by_id('pstad-price')
if ad["price_type"] != 'GIVE_AWAY':
text_area.send_keys(ad["price"])
price = driver.find_element_by_xpath("//input[@name='priceType' and @value='%s']" % ad["price_type"])
try:
price = driver.find_element_by_xpath("//input[@name='priceType' and @value='%s']" % ad["price_type"])
except NoSuchElementException:
try:
price = driver.find_element_by_xpath("//select[@name='priceType']/option[@value='%s']" % ad["price_type"])
except NoSuchElementException:
raise Exception('Cannot find price type selection!')
price.click()
Copy link
Contributor

@sebthom sebthom Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either the indention of the new try/catch block is wrong or the indention of price.click().

Your change will result in a NameError: name 'price' is not defined in case price_type == 'GIVE_AWAY'

fake_wait()

Expand Down Expand Up @@ -377,12 +421,6 @@ def post_ad(driver, ad, interactive):

fake_wait()

submit_button = driver.find_element_by_id('pstad-frmprview')
if submit_button:
submit_button.click()

fake_wait()

has_captcha = post_ad_has_captcha(driver)
if has_captcha:
if interactive:
Expand All @@ -393,6 +431,12 @@ def post_ad(driver, ad, interactive):
log.info("\tCaptcha input needed, but running in non-interactive mode! Skipping ...")
fRc = False

submit_button = driver.find_element_by_id('pstad-frmprview')
if submit_button:
submit_button.click()

fake_wait()

if fRc:
try:
submit_button = driver.find_element_by_id('prview-btn-post')
Expand Down Expand Up @@ -543,4 +587,4 @@ def signal_handler(sig, frame):
profile_write(sProfile, config)

driver.close()
log.info("Script done")
log.info("Script done")