这篇文章/问题是initial post that I asked a few or some days ago
的第二部分(非常感谢@alecxe对selenium webdriver api基础知识的精彩解释)
我有以下脚本读取文件(在其中列出了两个以上的单词或字符串)
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()
目前我正在发送列表中的一项(列表中转换的文件中的一个字符串或一个字)如果需要,我们可以在此视频中看到演示或工作流程demo
我希望将n个项目发送到此站点(Silabes划分应用程序)并接收在我的脚本/演示中处理过的每个项目。
有了这个目标,我一直在想以下几点:
(查看############新代码或思考#########部分)
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()
使用while指令,我的脚本会将所有单词发送到应用程序,以便处理这些单词并返回每个单词。 查看new demo(如果需要)
我确实希望分享这篇文章,并向大家询问以下内容:
还有另一种方法(可能更优化)执行同样的操作吗?列表理解可能吗?
我在这里介绍的while循环是一个无限循环...我可以改进这个代码部分,例如我的脚本完成正确的方法吗? 我想当我等到最后一个位置时...可能是我的脚本崩溃,虽然在这些测量中,金额变量总是比eb更大...
我会以更好的方式呈现结果,分割回来的单词
任何观察,建议或推荐(python最佳实践,想法或任何相关内容)都将非常受欢迎。
谢谢,并为长篇文章道歉。 :)
答案 0 :(得分:2)
for
循环来完成。amount
始终是项目数,永远不会更改。所以你永远不会停止进入while循环。考虑到这一点,我建议你应该保持你的代码干净,一致和简短。清洁代码的示例:
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)
format
处理格式化文本非常好:
output.write("My word sent is: {item}. "
"The word divide in silabes is: {result}\n".format(item=item, result=result))