from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium_stealth import stealth import os import time # Set download directory path download_dir = r"C:\Users\roberto.alves\Downloads\selenium_download" os.makedirs(download_dir, exist_ok=True) # Chrome options options = webdriver.ChromeOptions() options.add_experimental_option("prefs", { "download.default_directory": download_dir, "download.prompt_for_download": False, "directory_upgrade": True }) options.add_argument("--start-maximized") options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--disable-infobars") options.add_argument("--disable-notifications") options.add_argument("--disable-extensions") options.add_argument("--profile-directory=Default") options.add_argument("--disable-popup-blocking") options.add_argument("--disable-dev-shm-usage") options.add_argument("--no-sandbox") options.add_argument("--disable-features=IsolateOrigins,site-per-process") options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") options.add_argument('--enable-unsafe-swiftshader') # Create WebDriver instance driver = webdriver.Chrome(options=options) # Apply stealth settings stealth(driver, languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel Iris OpenGL", fix_hairline=True, ) # Navigate and login driver.maximize_window() driver.get('https://sgi.e-boticario.com.br/Paginas/Separacao/ConsultarPosicaoEstoque.aspx') wait = WebDriverWait(driver, 90) usuario = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/form/main/div[2]/div/div[2]/div[1]/input'))) usuario.send_keys( '15077405') print('usuario ok!') senha = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/form/main/div[2]/div/div[2]/div[3]/input'))) senha.send_keys('Admufal@2028') print('usuario ok!') ok = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/form/main/div[3]/button/span[1]'))) ok.click() print('enviar ok!') try: fechar_aviso = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="painelSuperior"]/div/a'))) fechar_aviso.click() print('foi try') except: fechar_aviso = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/form/div[4]/div/div/a'))) fechar_aviso.click() print('foi except') print('fechou aviso') try: posicao_estoque = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="menu-cod-7"]/a'))) posicao_estoque.click() posicao_estoque2 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="submenu-cod-7"]/div/div[1]/ul/li[3]/a'))) posicao_estoque2.click() posicao_estoque3 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="submenu-cod-7"]/div/div[1]/ul/li[3]/ul/li[4]'))) posicao_estoque3.click() print('foi posição try') except: print('foi posição except') posicao_estoque = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/form/div[4]/div[6]/div/div[1]/ul/li[3]/ul/li[4]/a'))) posicao_estoque.click() lupinha = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="ContentPlaceHolder1_LookupCodProduto_I2"]'))) lupinha.click() print('clicou na lupinha' ) pesquisar = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="ContentPlaceHolder1_LookupCodProduto_ctl00_TesteControleID_buscarButton_btn"]'))) pesquisar.click() registros_por_pg = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="ContentPlaceHolder1_LookupCodProduto_ctl00_TesteControleID_paginacao_registrosPorPaginaTextBox_T2"]'))) registros_por_pg.send_keys(Keys.BACKSPACE) registros_por_pg.send_keys(Keys.BACKSPACE) registros_por_pg.send_keys('50') registros_por_pg.send_keys(Keys.TAB) print('mudou pra 50') time.sleep(2) with open(r'C:\Users\roberto.alves\Downloads\selenium_download\ConsultaPosicaoEstoque', "a", encoding="utf-8") as file: file.write("Codigo,Situação,Ciclo_Lancamento") #try: xpath = "/html/body/form/div[4]/div[13]/div/div[1]/div[2]/div/div[1]/span[2]/div[1]/span[1]/span[2]/div[1]/div[2]/div[3]/div[2]/div/div/table/tbody/tr/td[2]/span/table/tbody/tr/td[11]" element = wait.until(EC.presence_of_element_located((By.XPATH, xpath))) print("Expected total:", element.text) #except: # print('deu erro na range x') time.sleep(2) rows = driver.find_elements(By.XPATH, "//table[contains(@id, 'produtosGrid')]//tr[td]") for x in range(2): #numero de paginas paginas for i in range(50): #linhas de texto por pagina print(x) for i, row in enumerate(rows[:50], start=1): # Limit to 50 rows tds = row.find_elements(By.CLASS_NAME, "grid_celula") try: print(tds[0]) except: print('n deu tds') codigo = driver.find_element(By.XPATH, f'/html/body/form/div[4]/div[13]/div/div[1]/div[2]/div/div[1]/span[2]/div[1]/span[1]/span[2]/div[1]/div[2]/div[3]/div[2]/div/div/div/div/table/tbody/tr[{i+1}]/td[2]').text print(codigo) #dados = str(tds) #codigo = driver.find_element(By.XPATH, f'/html/body/form/div[4]/div[13]/div/div[1]/div[2]/div/div[1]/span[2]/div[1]/span[1]/span[2]/div[1]/div[2]/div[3]/div[2]/div/div/div/div/table/tbody/tr[{i+1}]/td[2]').text situacao = driver.find_element(By.XPATH, f'/html/body/form/div[4]/div[13]/div/div[1]/div[2]/div/div[1]/span[2]/div[1]/span[1]/span[2]/div[1]/div[2]/div[3]/div[2]/div/div/div/div/table/tbody/tr[{i+1}]/td[8]').text ciclo = driver.find_element(By.XPATH, f'/html/body/form/div[4]/div[13]/div/div[1]/div[2]/div/div[1]/span[2]/div[1]/span[1]/span[2]/div[1]/div[2]/div[3]/div[2]/div/div/div/div/table/tbody/tr[{i+1}]/td[10]').text dados = list(codigo,situacao,ciclo) with open(r'C:\Users\roberto.alves\Downloads\selenium_download\ConsultaPosicaoEstoque', "w", encoding="utf-8") as file: file.write(dados) print('funcionou tudo!!!') time.sleep(30)