#🔒 Selenium Python how to run an XPath error handling exception on loop with multiple functions?

5 messages · Page 1 of 1 (latest)

crystal dragon
#

is it possible to schedule my code by running it with schedule.every(10).seconds.do(example) or similar? I'm trying to schedule the Error Handling part in my code (XPath) which works with a While loop although because its in a While loop it refuses to run the other def functions, I want the XPath/error handling part to run in a loop with my Selenium window but detect and not interfere with the other def functions. it should just detect the XPath being there or not being there then run an exception if it doesn't detect.. Does anyone have a solution?

My code:

def example():
    options = Options()
    options.add_experimental_option("detach", True)

    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),
                              options=options)

    driver.get("example.com")
    driver.set_window_position(0, 0)
    driver.set_window_size(750, 512)

    while True:
        e = driver.find_elements(By.XPATH,"/html/body/div/div/div/div/div[2]/div/div/div[2]/div[2]/div[1]/span/button/span")
        if not e:
            print("Element not found")
            pyautogui.moveTo(89, 56)
            time.sleep(1)
            pyautogui.click()

        time.sleep(10)


def example2():
    options = Options()
    options.add_experimental_option("detach", True)

    driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))

    driver.get("example.com")
    driver.set_window_position(750, 0)
    driver.set_window_size(750, 512)

    while True:
        e = driver.find_elements(By.XPATH,"/html/body/div/div/div/div/div[2]/div/div/div[2]/div[2]/div[1]/span/button/span")
        if not e:
            print("Element not found")
            pyautogui.moveTo(850, 57)
            time.sleep(1)
            pyautogui.click()

schedule.every().day.at("22:59").do(example)
schedule.every().day.at("22:59").do(example2)

while True:
    schedule.run_pending()
    time.sleep(1)
late duneBOT
#

@crystal dragon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

balmy burrow
#

multithrading?

late duneBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.