1

I am trying to loop csv rows into the selenium elements but program takes all rows and putting them into elements.

Here is the code

    def send_keys_textboxes(self,url):
        bot = self.bot
        bot.get(url)
        data = pd.read_csv("translations.csv")
        # I am not sure np is important but anyway
        langs = np.array(data)
        # These are clickable elements which opens popups
        elements = bot.find_elements_by_xpath("//a[@data-tag='globalize']")
        for elem in elements:
            class_of_element = elem.get_attribute("class")
            if class_of_element == 'cs-trans-icon':
                # Opens popup (panel)
                elem.click()
                time.sleep(3)
                # Select all textboxes
                textBoxes = bot.find_elements_by_tag_name('textarea')
                # Send values in a row to textboxes
                # PROBLEM HERE: It is sending 3 value in each textbox
                for phrase in langs:
                    for i in range(len(phrase)):
                        textBoxes[i].send_keys(phrase[i].title())

So, there are textboxes and csv of translations. I am sending first value to first textbox, second value to second textbox. But everytime it is sending all data inside csv.

For instance, my csv contains 3 rows and I have 3 panel with texboxes. First row must be send to first panel, second row must be send to second panel and so on..

So, value in rows are already sending to textboxes but as I stated it is all 3 rows, I need to loop somehow to make it work properly.

enter image description here

1 Answer 1

1

Your nested for loop for phrase in langs: is the reason 3 values are getting sent to each text box. You are performing send_keys 3 times on each textbox, according to this loop.

Based on your updated description -- each CSV row is associated with its own popup. Once the popoup gets opened, we need to iterate the values within a row and send the value to a textbox -- first value goes to first textbox, second value goes to second textbox, etc.

def send_keys_textboxes(self,url):

    bot = self.bot
    bot.get(url)

    data = pd.read_csv("translations.csv")
    langs = np.array(data)

    # declare an index to keep track of which csv ROW we are on
    row_index = 0;

    # These are clickable elements which opens popups
    elements = bot.find_elements_by_xpath("//a[@data-tag='globalize']")

    for elem in elements:
        class_of_element = elem.get_attribute("class")
        if class_of_element == 'cs-trans-icon':

            # Opens popup (panel)
            # this popup needs values from langs[row_index]
            elem.click()
            time.sleep(3)

            # Get all textboxes for this popup
            textBoxes = bot.find_elements_by_tag_name('textarea')  

            # declare an index to keep track of which csv VALUE we are on
            value_index = 0

            # once popup is opened, loop through CSV values within a row
            for value in langs[row_index]:

                # send value[value_index] text to textBoxes[value_index]        
                textBoxes[value_index].send_keys(value[value_index])

                # increment value_index to move on to next textBox & value
                value_index++

            # increment row_index to move on to next CSV row once we finish with this popup
            row_index++

This loop iterates your list of elements -- once we locate a popup element and open it, we are reading a single row from the CSV. Within the popup, we grab textbox elements. Then, we iterate the text box elements and values within CSV row and send text from a value into a textbox, using value_index to keep track of which value goes to which textbox.

row_index keeps track of the CSV ROW we are looking at, and this gets incremented with each popup -- so, row 1 will go to first popup, row 2 goes to second popup, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for great explanation! Now, it is sending csv rows correctly. But it is sending all values of a row into first textbox of first popup, when second row starting it is sending all values of row into second textbox of second popup. I think one more loop needs to iterate over these texboxes.
I think I’m a bit confused here — so there are multiple pop ups, and each pop up has multiple text boxes, and you want to send the CSV data to each text box, for each pop up? That makes sense too, I just want to make sure I am understanding correctly.
Yes exactly what I am trying to expalin :) First csv row -> to first popup's textboxes, second csv row -> to second popup's textboxes and so on
That makes sense — I’ve refactored my answer based on this. Now, we start index over every time we open a popup. This should send CSV values to appropriate text boxes in a loop, for every popup that gets opened. Let me know if this works for you.
Well, it's really close :D Take a look screenshot above (I edited question). This is the first popup and in first textbox you can see first row in csv going in it. I need loop these phrases (the words inside first textbox) to all textboxes. In second textbox you can see second row in csv going inside it. However, second row should loop to second popup's textboxes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.