bgarcial
bgarcial

Reputation: 3213

Sending and Receive data from a web page - Selenium WebDriver API

This post/question is the second part of a initial post that I asked a few or some days ago

(Thanks a lot for @alecxe for their great explanation about of selenium webdriver api fundamentals)

I have the following script which read a file (inside it there is listed two o more words or strings)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
# Website application that I want reach for process data
driver.get("http://tip.iatext.ulpgc.es/silabas/Default.aspx")

# Open the file for read it.
with open('Diccionario.txt','r') as f:
    list = []

    # Browse the file and turn on at list
    for item in f:
        list.append(item)
    print (list)

    # Setup the len of list
    amount = (len(list))
    #print (amount)

    # I get the first item of the list 
    current_word = list[0]
    #print (current_word)

# Send string through selenium WebDriver API
elem = driver.find_element_by_id("MainContent_TextBox1")
elem.send_keys(current_word)

#elem.send_keys(Keys.RETURN)
f.close()

# Locate the element (tag, css, etc) by which I want ask for receive information  
result = driver.find_element_by_css_selector(
"table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)").text
print(result)

# Write the information received  in a text file
newfile = open('output.txt','a')
newfile.write(result)
newfile.close()

Currently I am sending one item of the list (one string or one word of the file converted on list) If you want, we can see the demo or workflow in this video demo

I want send n items to this site (Silabes divide application) and receive each one of them processed back in my script/demo.

With this goal, I had been thinking the following:

(View the ############ New code or kind of think ######### section)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://tip.iatext.ulpgc.es/silabas/Default.aspx")

with open('Diccionario.txt','r') as f:
    list = []
    for item in f:
        list.append(item)
    print (list)

    amount = (len(list))
    #print (amount)
    i=0

    ############ New code or kind of think #########

    while amount > 0:
        print ("Value position", i)

        # I get the first item on list
        current_word = list[i]
        print(current_word)
        ############ -- #####
        # Send the data to the web site silabes divide application
        elem = driver.find_element_by_id("MainContent_TextBox1")
        elem.send_keys(current_word)
        #elem.send_keys(Keys.RETURN)
        f.close()

        #Ask for the result inside specifics tags or css
        result = driver.find_element_by_css_selector("table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)").text
        print("My word sent is:", " ", current_word + '\n'
        "The word divide in silabes is:", " ", result)
        # Increment i
        i+=1
        print(i)

        #Write the results into a file
        newfile = open('output.txt','a')
        newfile.write('\n' + '\n' + "My word sent is:" + " " + current_word +
                  " " + "The word divide in silabes is:" + " " + result)
        newfile.close()

With the while instruction, my script send all words to the application for that these to be processed and return one per one. View the new demo (if you want)

I did want share this post, with the order of ask to everyone the following:

  1. There is some another way (more optimized may be) of perform this same action? list comprehensions maybe?

  2. My while loop presented here, is an infinite loop ... Can I improve this code section, for example for that my script finish of a correctly way? I guess that when i to be equal to last position ... possibly my script crash, although in these measure, the amount variable always to eb greater than cero ...

  3. I will should present of a better way the result, splitting the silabes words returned

Any observation, suggestion or recommendation (python best practices, ideas or anything about it) it would be hugely appreciated.

Thanks and my apologies for the long post. :)

Upvotes: 1

Views: 1405

Answers (1)

Osher
Osher

Reputation: 46

  1. list comprehension doesn't needed here, as you just need to iterate over a list. And this can be done by a simple for loop.
  2. amount is always the number of items, and never changes. So you will never stop getting into the while loop.
  3. At the end there's a code sample.

With that in mind, I suggest that you should keep your code clean, consistent and short. An example for a cleaner code:

with open('Diccionario.txt', 'r') as input, \
        open('output.txt', 'w') as output:
    for item in input:
        element = driver.find_element_by_id("MainContent_TextBox1")
        element.send_keys(item)

        result = driver.find_element_by_css_selector("table#MainContent_Table1 > tbody > tr > td:nth-of-type(2)").text
        output.write(result)

Formatting text is very nice handled by format:

output.write("My word sent is: {item}. "
             "The word divide in silabes is: {result}\n".format(item=item, result=result))

Upvotes: 2

Related Questions