0

Can someone please show me hot to run login() only once inside this Loop? Firts time it will open https://mywebsite.com/1234 and ask for a login, login() will do this, then it will go and download .xlsx files. Then it will go to https://mywebsite.com/4567 and login() will try to do it's job again, but there's no need. So il will set an error as, it can't find username field.

Is there any way so login would run only 1st time ?

Help please!

web_link='https://mywebsite.com/'
    tt_nr=TT_usrinput.get(0.1, END)
    wait = WebDriverWait(self,10)



    t=tt_nr.split('\n')

    for i in t:

        if i != "":
            fin=web_link+i
            self.maximize_window()
            self.delete_all_cookies()
            self.get(fin)
            
            login()
            

            WebDriverWait(self, 30).until(EC.element_to_be_clickable((By.ID, 'tab_related_items')))  # click Related Items
            self.find_element_by_id('tab_related_items').click()

            WebDriverWait(self, 5).until(
                EC.element_to_be_clickable((By.XPATH, '//a[@target="_blank" and contains(@href, ".xlsx")]')))
            elemsxlsx = self.find_element_by_xpath('//a[@target="_blank" and contains(@href, ".xlsx")]')
            elemsxlsx.click()

madacmjtr
  • 13
  • 6
  • Set some variable to False outside the loop. Before login, test that flag. If False, do the login and then set it to True –  Jul 22 '21 at 14:08

1 Answers1

0

As Andy Knight said, just add a flag variable outside of the loop and check it in the loop.

web_link='https://mywebsite.com/'
    tt_nr=TT_usrinput.get(0.1, END)
    wait = WebDriverWait(self,10)

do_login = True


    t=tt_nr.split('\n')

    for i in t:

        if i != "":
            fin=web_link+i
            self.maximize_window()
            self.delete_all_cookies()
            self.get(fin)
            
            if do_login:
               login()
               do_login = False
            

            WebDriverWait(self, 30).until(EC.element_to_be_clickable((By.ID, 'tab_related_items')))  # click Related Items
            self.find_element_by_id('tab_related_items').click()

            WebDriverWait(self, 5).until(
                EC.element_to_be_clickable((By.XPATH, '//a[@target="_blank" and contains(@href, ".xlsx")]')))
            elemsxlsx = self.find_element_by_xpath('//a[@target="_blank" and contains(@href, ".xlsx")]')
            elemsxlsx.click()
CaioT
  • 1,973
  • 1
  • 11
  • 20