使用 selenium 库获取循环的问题

问题描述 投票:0回答:1

我使用 Selenium 和 Pyautogui 编写了这段代码,基于 Excel 列表来清理共享点列表。程序第一次运行正常,但我无法使循环工作,我不知道我要做什么。有人可以帮助我吗?

from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pyautogui

time.sleep(5)
num_repeticoes = 800
repeticao_atual = 0

# Inicialize o navegador Microsoft Edge
navegador = webdriver.Edge()

navegador.get("https://mdiasbranco.sharepoint.com/sites/Controledearmazem-MDB/Lists/movimentacaoPallet_JBO/AllItems.aspx")
navegador.maximize_window()

while repeticao_atual < num_repeticoes:
    try:
        page_loaded_selector = 'body'  

        # Esperar até que a página esteja carregada completamente
        WebDriverWait(navegador, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, page_loaded_selector)))
        # aperta no title
        linktitle_xpath = '//*[@id="header15-title_1267"]'
        linktitle = WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.XPATH, linktitle_xpath)))

        # Clicar no filtrarpor dentro do title
        linktitle.click()
        
        time.sleep(1)
        botao_filtrar_xpath = '/html/body/div[2]/div/div/div/div/div/div/ul/li[4]/button'
        botao_filtrar = WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.XPATH, botao_filtrar_xpath)))

        # Clicar no botão
        botao_filtrar.click()

        time.sleep(1)

        # mudapra planilha
        pyautogui.hotkey('alt', 'tab')
        time.sleep(1)
        pyautogui.click(x=114, y=236)
        time.sleep(1)
        pyautogui.hotkey('ctrl', 'c')
        time.sleep(1)
        pyautogui.hotkey('alt', 'tab')
        time.sleep(1)

        campo_filtro_xpath = '//*[@id="combobox-id__824"]'
        campo_filtro = WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.XPATH, campo_filtro_xpath)))
        pyautogui.click(x=1049, y=205)
        pyautogui.hotkey('ctrl', 'v')
        time.sleep(2)
        pyautogui.press('enter')

        # botao aplicar
        executar_xpath = '/html/body/div[2]/div/div/div/div[2]/div[2]/div/div[3]/div/div/button[1]'
        botao_filtrar2 = WebDriverWait(navegador, 10).until(EC.element_to_be_clickable((By.XPATH, executar_xpath)))

        # Clicar no botão
        botao_filtrar2.click()
        time.sleep(6)

        # selciona todos para excluir
        pyautogui.click(x=338, y=437)
        time.sleep(2)
        
        # aperta os tres pontinhos
        pyautogui.click(x=790, y=267)
        
        # aperta em excluir
        pyautogui.click(x=795, y=304)
        time.sleep(2)
        # confirma a exclusão
        pyautogui.click(x=712, y=504)
        time.sleep(2)
        # tira o filtro
        pyautogui.click(x=1241, y=398)
        time.sleep(2)
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        pyautogui.click(x=16, y=237, button='right')
        time.sleep(2)
        pyautogui.click(x=73, y=459)
        pyautogui.hotkey('alt', 'tab')
        time.sleep(1)
        
        repeticao_atual += 1

    except Exception as e:
        print(f"Erro na repetição {repeticao_atual + 1}: {e}")

    finally:
        # Feche o navegador ao final do processo, independentemente de qualquer exceção
        navegador.quit()

我只是想让这个程序循环,但做不到。我确实尝试了很多事情,但只跑了一次就停下来了。

python python-2.7 selenium-webdriver web-scraping selenium-chromedriver
1个回答
0
投票

此处的 finally 语句关闭浏览器。 删除最后并添加

同时:

 try:

 except:

而正确:

通过

© www.soinside.com 2019 - 2024. All rights reserved.