我刚刚开始探索selenium,它很棒,但是我想写一个脚本,它将使用ELinks来浏览网页。
使用Se webdriver,我可以这样做:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#Open firefox
br=webdriver.Firefox()
#open website
br.get('http://python.org')
#check if website title contains word
assert 'Python' in br.title
elem=br.find_element_by_name('q')
elem.send_keys('selenium')
elem.send_keys(Keys.RETURN)
assert 'Google' in br.title
br.close()但是如何在python中使用Elink来完成类似的任务呢?(或者这根本就是可能的?)
发布于 2012-10-19 15:49:57
elinks真的很快。您可以使用Lua脚本语言进行automate elinks。他们的文档里有example scripts。您也可以尝试pexpect,这是在python中自动化终端应用程序的一种非常好的方法。下面的脚本使用pexpect执行与问题中的示例相同的任务。它将访问python.org,搜索selenium,将搜索结果保存到一个文件,然后退出链接。
from pexpect import spawn
import time
import datetime
KEY_UP = '\x1b[A'
KEY_DOWN = '\x1b[B'
KEY_RIGHT = '\x1b[C'
KEY_LEFT = '\x1b[D'
KEY_ESCAPE = '\x1b'
KEY_BACKSPACE = '\x7f'
child = spawn('elinks http://python.org')
print 'waiting for python.org to load'
child.expect('Python')
time.sleep(0.1)
print 'doing selenium search'
child.sendline('/advanced search')
child.sendline(KEY_UP * 2)
child.sendline('selenium')
child.sendline('')
print 'waiting for search results'
child.expect('Google Search')
time.sleep(0.1)
print 'saving html'
child.send(KEY_ESCAPE) # bring up menu
child.send(KEY_DOWN + 's') # select save as in menu
child.send(KEY_BACKSPACE * 100) # remove any file name already in input box
file = './saved_' + datetime.datetime.now().strftime('%H%M%S') + '.html'
child.sendline(file)
#child.interact() #uncomment to interact with elinks, good for debugging
print 'quiting'
child.sendline('q')
child.wait()发布于 2012-10-19 01:15:56
Selenium有一个无头浏览器选项htmlunit。htmlunit仍然没有完整的javascript实现,但它有很多实现--你可以一直使用它,直到它崩溃为止。
或者,Chrome是非常快的,不管怎样。我使用chrome实现webdriver自动化,Chrome的速度足够快,如果我没有正确编码计时,我最终会遇到端口耗尽的情况。
https://stackoverflow.com/questions/12959805
复制相似问题