AutoPercenty/edit/price.py

187 lines
9.9 KiB
Python

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
from selenium.webdriver.common.keys import Keys
import time
from edit.action_elements import click_element, return_element, click_and_confirm_tab
from edit.price_cal import calculate_margin_and_price
# from ai.compare import find_most_similar_image_by_one
import logging
# 로거 인스턴스 가져오기
logger = logging.getLogger('default_logger')
def modify_price_page(driver, product_infos):
thumb_data_note = "2"
click_and_confirm_tab(driver, thumb_data_note, 10)
# 가격 탭으로 이동
logger.debug("가격탭으로 이동")
# option_tab_XPATH = "//div[@id='rc-tabs-0-tab-3']"
# keyword_tab_CSS = ".ant-tabs-tab:nth-child(3)"
# click_element(driver, 'CSS_SELECTOR', keyword_tab_CSS, 10, 'js')
logger.debug("가격탭으로 이동 완료")
logger.debug("페이지 로딩 대기")
time.sleep(2) # 페이지 로딩 대기.
try:
plus_fee_xpath = "//div[@id='productMainContentContainerId']/div/div/div/div/div[2]/div/div/div[8]/div/div/div[3]/div/div/div/div/div[2]/input"
plus_fee_css = "div#productMainContentContainerId div:nth-child(8) > div > div > div:nth-child(3) > div > div > div > div.ant-input-number.css-1li46mu.ant-input-number-outlined > div.ant-input-number-input-wrap > input"
plus_fee_element = return_element(driver, 'XPATH', plus_fee_xpath, 10)
plus_fee_element_by_css = return_element(driver, 'CSS', plus_fee_css, 10)
plus_fee_text = plus_fee_element.text
print(f"plus_fee_text : {plus_fee_text}")
plus_fee_by_css_text = plus_fee_element_by_css.text
print(f"plus_fee_by_css_text : {plus_fee_by_css_text}")
option_high_price = product_infos.option_high_price
option_low_price = product_infos.option_low_price
# tao_high_price = product_infos.tao_high_price # 디버깅을 위해 선택된 옵션가격이 아닌 기존의 원래가격을 가져옴
# tao_low_price = product_infos.tao_low_price # 디버깅을 위해 선택된 옵션가격이 아닌 기존의 원래가격을 가져옴
# naver_low_price = product_infos.naver_low_price
# naver_avg_price = product_infos.naver_avg_price
# naver_high_price = product_infos.naver_high_price
w_delv_fee = product_infos.w_delv_fee
packing_fee = product_infos.packing_fee
inputs = {
"option_low_price": option_low_price,
"option_high_price": option_high_price,
"delv_fee": w_delv_fee,
"packing_fee": packing_fee,
# "ns_low_price": naver_low_price,
# "ns_high_price": naver_high_price,
"plus_fee_text": plus_fee_text,
}
logger.debug(f"가격계산 INPUT 요소 : {inputs}")
except Exception as e:
logger.error(f"가격계산 INPUT 요소 계산 중 에러 발생 {e}", exc_info=True)
try:
outputs = calculate_margin_and_price(inputs)
logger.debug(f"가격계산 OUTPUT 요소 : {outputs}")
selling_price = outputs["selling_price"]
final_margin_rate = outputs["final_margin_rate"]
seller_cost = outputs["seller_cost"]
plus_margin = outputs["plus_margin"]
# ns_avg_price = outputs["ns_avg_price"]
return_fee = round((selling_price*0.4) / 1000) * 1000
init_delv_fee = seller_cost
exchange_fee = return_fee + init_delv_fee
product_infos.plus_fee = plus_margin
product_infos.return_fee = return_fee
product_infos.init_delv_fee = init_delv_fee
product_infos.exchange_fee = exchange_fee
logger.debug(f"결정된 상품가격: {selling_price}원, 최종 마진율: {final_margin_rate:.2f}%")
logger.debug(f"판매자 원가: {seller_cost}")
logger.debug(f"더하기 마진: {plus_margin}")
# logger.debug(f"네이버 평균가: {ns_avg_price}원")
except Exception as e:
logger.error(f"가격계산 OUTPUT 요소 계산 중 에러 발생 {e}", exc_info=True)
# # 가격 탭으로 이동
# logger.debug("내부함수 가격 탭으로 이동")
# thumb_tab_CSS = '.ant-tabs-tab:nth-child(3)'
# click_element(driver, 'CSS_SELECTOR', thumb_tab_CSS, 10, 'js')
# logger.debug("내부함수 가격 탭으로 이동 완료")
# logger.debug("페이지 로딩 대기")
# time.sleep(2) # 페이지 로딩 대기.
try:
logger.debug("더하기마진 수정")
# plus_fee_xpath="//div[@id='productMainContentContainerId']/div/div/div/div/div[2]/div/div/div[8]/div/div/div[3]/div/div/div/div/div[2]/input"
# # plus_fee_element = return_element(driver, 'XPATH', plus_fee_xpath, 10)
plus_fee_element.send_keys(Keys.CLEAR) # 기존 가격 삭제
driver.execute_script("arguments[0].value = '';", plus_fee_element) # 기존 가격 삭제 JS
logger.debug("기존가격 삭제")
plus_fee_element.send_keys(plus_margin) # 새 가격 입력
logger.debug(f"더하기마진 수정 완료 : {plus_margin}")
except Exception as e:
logger.error(f"더하기마진 수정 중 에러 발생 {e}", exc_info=True)
try:
logger.debug("해외배송비 수정")
fore_delv_fee_xpath="//div[@id='productMainContentContainerId']/div/div/div/div/div[2]/div/div/div[10]/div/div/div/div/div[2]/input"
fore_delv_fee_element = return_element(driver, 'XPATH', fore_delv_fee_xpath, 10)
fore_delv_fee_element.send_keys(Keys.CLEAR) # 기존 가격 삭제
driver.execute_script("arguments[0].value = '';", fore_delv_fee_element) # 기존 가격 삭제 JS
logger.debug("기존가격 삭제")
# fore_delv_fee_element.send_keys(w_delv_fee) # 새 가격 입력
fore_delv_fee_element.send_keys(0) # 더하기마진을 최종가격으로 가져오면서 배송비는 0원으로 계산됨. 더하기 마진에 배송비가 모두 포함됨.
# logger.debug(f"해외배송비 수정 완료 : {w_delv_fee}")
logger.debug(f"해외배송비 수정 완료 : {w_delv_fee}")
logger.debug("더하기마진을 최종가격으로 가져오면서 배송비는 0원으로 계산됨. 더하기 마진에 배송비가 모두 포함됨.")
except Exception as e:
logger.error(f"해외배송비 수정 중 에러 발생 {e}", exc_info=True)
try:
logger.debug("반품비 수정")
return_fee_xpath = "//div[@id='productMainContentContainerId']/div/div/div/div/div[4]/div/div/div[3]/div/div/div/div/div[2]/input"
return_fee_element = return_element(driver, 'XPATH', return_fee_xpath, 10)
return_fee_element.send_keys(Keys.CLEAR) # 기존 가격 삭제
driver.execute_script("arguments[0].value = '';", return_fee_element) # 기존 가격 삭제 JS
logger.debug("기존가격 삭제")
return_fee = 199000
return_fee_element.send_keys(return_fee) # 새 가격 입력
logger.debug(f"반품비 수정 완료 : {return_fee}")
except Exception as e:
logger.error(f"반품비 수정 중 에러 발생 {e}", exc_info=True)
try:
logger.debug("초도배송비 수정")
first_delv_fee_xpath = "//div[@id='productMainContentContainerId']/div/div/div/div/div[4]/div/div/div[4]/div/div[2]/div/div/div[2]/input"
first_delv_fee_element = return_element(driver, 'XPATH', first_delv_fee_xpath, 10)
first_delv_fee_element.send_keys(Keys.CLEAR) # 기존 가격 삭제
driver.execute_script("arguments[0].value = '';", first_delv_fee_element) # 기존 가격 삭제 JS
logger.debug("기존가격 삭제")
init_delv_fee = 199000
first_delv_fee_element.send_keys(init_delv_fee) # 새 가격 입력
logger.debug(f"초도배송비 수정 완료 : {init_delv_fee}")
except Exception as e:
logger.error(f"초도배송비 수정 중 에러 발생 {e}", exc_info=True)
try:
logger.debug("교환비 수정")
exchange_fee_xpath = "//div[@id='productMainContentContainerId']/div/div/div/div/div[4]/div/div/div[5]/div/div/div/div/div[2]/input"
exchange_fee_element = return_element(driver, 'XPATH', exchange_fee_xpath, 10)
exchange_fee_element.send_keys(Keys.CLEAR) # 기존 가격 삭제
driver.execute_script("arguments[0].value = '';", exchange_fee_element) # 기존 가격 삭제 JS
logger.debug("기존가격 삭제")
exchange_fee = 499000
exchange_fee_element.send_keys(exchange_fee) # 새 가격 입력
logger.debug(f"교환비 수정 완료 : {exchange_fee}`")
except Exception as e:
logger.error(f"교환비 수정 중 에러 발생 {e}", exc_info=True)
# logger.debug("기본마진율 수정")
# earning_rate_xpath = "//div[@id='productMainContentContainerId']/div/div/div/div/div[2]/div/div/div[8]/div/div/div/div/div/div/div/div[2]/input"
# earning_rate_element = return_element(driver, 'XPATH', earning_rate_xpath, 10, 'js')
# earning_rate_element.send_keys(Keys.CLEAR) # 기존 가격 삭제
# driver.execute_script("arguments[0].value = '';", earning_rate_element) # 기존 가격 삭제 JS
# earning_rate_element.send_keys('new_Price') # 새 가격 입력
# background: rgb(240, 240, 240); box-shadow: none; color: rgba(0, 0, 0, 0.85);
# 백그라운드 컬러로 빨간 가격탭 찾기
# background: rgb(255, 77, 79); box-shadow: none; color: rgb(255, 255, 255);
try:
save_xpath="//button[contains(.,'저장하기')]"
click_element(driver, 'XPATH', save_xpath, 10, 'js')
logger.debug("가격 정리 후 저장버튼 클릭 완료")
except Exception as e:
logger.error(f"가격 정리 저장버튼 클릭 중 에러 발생 {e}", exc_info=True)