Lieutenant Dan
Lieutenant Dan

Reputation: 8294

run click function AFTER a series of for and if statements

So, as you can see in the below code; I have a series of loops - starting with a for loop, then if, if else elif try -- I am using these all to populate a 1-page web form with Python and Selenium. This is done; now I am just trying to add a click function AFTER this code has ran (i.e. populated my web form) in order to click my 'Save Button' then 'Confirm Save'.

Currently I have tried adding the click function in except last else and pass and no matter what it still fires before the code finishes populating the form. I've also tried implicit and explicit wait.

def add_assessment(self, record, map_data):
    """Create contact log"""
    qna_frame = self.driver.find_element_by_css_selector("iframe[id^='iccc']")
    self.driver.switch_to.frame(qna_frame)

    pages = self.driver.find_element_by_css_selector("ul[class='nav nav-pills nav-stacked qna-tabs']")
    pages = pages.find_elements_by_css_selector("a")

    for page in pages:
        page.click()

        questions = self.driver.find_elements_by_css_selector("fieldset")
        questions = [question for question in questions if question.text not in  ("", " ", None)]

        for question in questions[1:]:
            self.q_text = question.find_element_by_css_selector("span[class='question-text ng-binding']").text
            questionType = map_data.loc[map_data['question_text'] == self.q_text, 'question_type'].item()

            answer = map_data.loc[map_data['question_text'] == self.q_text, 'map'].item()
            answer = getattr(record, answer)

            if answer not in ("", " ", "NaT", "NaN", None):
            # while answer != "" and answer != " " and answer != "NaT":

                if questionType == 'checks':
                    self.choose_checks(question, answer)
                else:
                    try:
                        if questionType == 'text':
                            self.driver.implicitly_wait(0)

                            (question.find_element_by_css_selector("textarea").send_keys(str(answer))
                            if 
                                question.find_elements_by_css_selector("textarea")
                            else 
                                question.find_element_by_css_selector("input").send_keys(answer))

                            self.driver.implicitly_wait(15)

                        elif questionType == 'date':
                            try:
                                answer = answer.strftime('%m/%d/%Y')
                                question.find_element_by_css_selector("input").send_keys(answer)
                                page.click()

                            except Exception as e:
                                raise Errors.RequiredDataError('Issues with Assessment Date -- {}'.format(e))
                        elif questionType == 'radio':
                            question.find_element_by_css_selector("input[value='{}']".format(answer)).click()
                    except:
                        continue                              
                    else:
                        # driver.find_element_by_css_selector("#publishButton").click()
                        pass   

Upvotes: 0

Views: 34

Answers (1)

TheN00bBuilder
TheN00bBuilder

Reputation: 124

Put that at the end of the function, with the word function meaning that little def statement that starts it. When something breaks a loop, it goes back to the indentation level that the loop started at. It then keeps going until it sees something within its scope, which is not in that loop, as it already broke the loop. In other words, loops are a one time thing. Once it's broken, it stops the loop and continues from where the loop started. You want the final statement to be at the end of the function and on the same indentation level as the line for page in pages:.

Upvotes: 1

Related Questions