Home > other >  Python loops, need some advice
Python loops, need some advice

Time:11-26

i trying to create a small program usign python and i need some help about python loops. this small program will automates a fairly boring repetitive task.

I use module : selenium, time and pyautogui

here is the piece of code that i want to repeat until it no longer finds a certain element on the web page :

btnOptions = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[5]/div/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div[2]/div/div[2]/div[3]/div[1]/div/div/div/div/div/div/div/div/div/div/div[8]/div/div[2]/div/div[3]/div/div")
btnOptions.click()
time.sleep(1)
pyautogui.press("down", presses=10)
pyautogui.press("enter")
time.sleep(1)
btn_move = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[6]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div[3]/div/div/div/div[1]")
btn_move.click()

as long as the btn_option is found, the program must continue otherwise it must stop.

I can't find a solution, if anyone can help me it would be very appreciated. thanks a lot

i tried several loops but every time i get an error of course, I'm just new to coding in python :/

CodePudding user response:

I've done a similar task. You may try this:

while True:
    try:
        btnOptions = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[5]/div/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div[2]/div/div[2]/div[3]/div[1]/div/div/div/div/div/div/div/div/div/div/div[8]/div/div[2]/div/div[3]/div/div")
        btnOptions.click()
        time.sleep(1)
        pyautogui.press("down", presses=10)
        pyautogui.press("enter")
        time.sleep(1)
        btn_move = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[6]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div[3]/div/div/div/div[1]")
        btn_move.click()
    except NoSuchElementException:
        break
  • Related