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, re, random 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_product_title(driver, product_infos, login_info): thumb_data_note = "0" click_and_confirm_tab(driver, thumb_data_note, 10) # 가격 탭으로 이동 logger.debug("내부함수 상품명 탭으로 이동") title_tab_CSS = '.ant-tabs-tab:nth-child(1)' click_element(driver, 'CSS_SELECTOR', title_tab_CSS, 10, 'js') logger.debug("내부함수 상품명 탭으로 이동 완료") logger.debug("페이지 로딩 대기") time.sleep(2) # 페이지 로딩 대기. #상품명 복사 # 원본상품명 tao_title_text = "//div[@id='productMainContentContainerId']/div/div/div[6]/div/div/span" original_title_xpath="//div[@id='productMainContentContainerId']/div/div/div[6]/div[2]/div/span" # product_title_element = driver.find_element(By.XPATH, "//div[@id='productMainContentContainerId']/div/div/div/div/div[5]/div/span/input") try: product_title_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//div[5]/div/span/input")) ) except Exception as e: logger.debug(f"상품명 복사 중 오류 발생: 요소를 찾을 수 없습니다. : {e}", exc_info=True) # product_infos에 저장된 최초 상품명과 옵션명들을 이용해 새로운 상품명을 조합 new_title = set_title(product_infos) product_title_element.set_attribute(new_title) save_xpath="//button[contains(.,'저장하기')]" click_element(driver, 'XPATH', save_xpath, 10) logger.debug("옵션 정리 후 저장버튼 클릭 완료") def set_title(product_infos): current_title = product_infos.init_title new_title = "" # 상품명을 공백 기준으로 분리 current_title_parts = re.split(r' ', current_title) # 숫자 포함 단어 제외, 영어와 숫자로만 이루어진 단어 제외 filtered_parts = [part for part in current_title_parts if not re.search(r'\d', part) and not re.fullmatch(r'[A-Za-z0-9]+', part)] # 세 번째 단어부터 나머지 단어 섞기 if len(filtered_parts) >= 3: shuffled_parts = filtered_parts[2:] random.shuffle(shuffled_parts) new_title_parts = filtered_parts[:2] + shuffled_parts else: new_title_parts = filtered_parts # 옵션명에서 중요 단어 추출 후 상품명에 추가 option_keywords = [keyword for part in product_infos.trans_option_name_parts for keyword in part if keyword in ['색상', '크기', '용량']] option_keywords = option_keywords[:2] # 대표하는 단어 2개만 선택 # 최종 상품명 조합 new_title = ' '.join(new_title_parts + option_keywords) return new_title