python htmlunit怎么定位元素
2016-05-09
展开全部
用打开浏览器的方式(如火狐)测试代码如下,该代码来源虫师博客,调试通过
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
browser.find_element_by_xpath("//a[@data-bk='5174.1']")
print"ok,test is pass!"
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.close()
运行结果:ok,test is pass!
=====================================================================
若觉得慢,可以用HtmlUnit
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities #(1)
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Remote(desired_capabilities=DesiredCapabilities.HTMLUNIT) #(2)
#browser = webdriver.DesiredCapabilities.HTMLUNIT() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
browser.find_element_by_xpath("//a[@data-bk='5174.1']")
print"ok"
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.close()
运行结果:ok
p.s. (1),(2)两处目前还不太明白,但程序调通,以后慢慢理解。
======================================================
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
browser.find_element_by_xpath("//a[@data-bk='5174.1']")
print"ok,test is pass!"
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.close()
运行结果:ok,test is pass!
=====================================================================
若觉得慢,可以用HtmlUnit
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities #(1)
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Remote(desired_capabilities=DesiredCapabilities.HTMLUNIT) #(2)
#browser = webdriver.DesiredCapabilities.HTMLUNIT() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
browser.find_element_by_xpath("//a[@data-bk='5174.1']")
print"ok"
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.close()
运行结果:ok
p.s. (1),(2)两处目前还不太明白,但程序调通,以后慢慢理解。
======================================================
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询