0

I have a simple Python/Selenium script that has to wait for 4 checks to happen. Each of these on a separated thread. On the first thread that returns information, the program should continue and the remaining threads killed (if possible).

I have a program that is creating the threads just fine, but I have no idea on how to continue on the first that have executed.

these are my function definitions:

def wait_by(search_type, locator, timeout):
    WebDriverWait(driver, timeout).until(EC.presence_of_element_located((search_type, locator)))

def multi_locator_verify_element(xpath_locator, css_locator, name_locator, id_locator, timeout):
    element = ""

    if xpath_locator != "":
      tx = Thread(target=wait_by, args=(By.XPATH, xpath_locator, timeout))
       tx.start()
   if css_locator != "":
       tc = Thread(target=wait_by, args=(By.CSS, css_locator, timeout))
       tc.start()
  if name_locator != "":
       tn = Thread(target=wait_by, args=(By.NAME, name_locator, timeout))
       tn.start()
   if id_locator != "":
       ti = Thread(target=wait_by, args=(By.ID, id_locator, timeout))
       ti.start()

The multi_locator_verify_element function should finish after the first thread has been able to retrive the right element.

Could someone please help me with that?

Regards,

  • Note that you're probably not going to be able to safely share a webdriver instance across threads (or processes at all). Aside from that, [a `ThreadPoolExecutor` with `as_completed`](https://stackoverflow.com/a/26590886/833881) seems like it should do what you're looking for. `futures` also has process support in `ProcessPoolExecutor`, and `multiprocessing` has Pool for something similar. – kungphu Sep 25 '18 at 05:54
  • Thanks, I will check for that. I have been dealing with very complex scripts and would like to try several locators at once and continue on the first one that succeeds. I will keep your note on the WebDriver. Thanks. – Igor Simoes Oct 09 '18 at 18:25

0 Answers0