java.lang.NullPointerException
at com.learningselenium.pageobject.MessagePage.enterMessageBox(MessagePage.java:33)
at com.learningselenium.pageobject.MessagePage.SendMessage(MessagePage.java:40)
at com.learningselenium.pageobject.testMessageWithPageObject.test(testMessageWithPageObject.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
这本书里的例子
空指针错误,你做了一个click 事件,但是如果那个message的对象没有获取到对应的按钮,则会报这样的错!
为什么会没有获取到对象呢
@sarua:
@sarua:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'acmen'
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("http://passport.cnblogs.com/user/signin?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2F")
user_name = driver.find_element_by_id("input1")
user_name.clear()
user_name.send_keys("用户名")
password = driver.find_element_by_id("input2")
password.clear()
password.send_keys("密码")
driver.find_element_by_id("signin").click()
sleep(5)
driver.find_elements_by_partial_link_text(u"短消息")[0].click()
这个是我使用python做的 可以跑出来
有两点注意
1. 登陆进去之后 sleep一会再去获取短信息连接
2. 使用文字获得的是一个列表只取第一个(python是这样)
建议
获取页面元素最好采用
xpath
id
class name
@Acmen:
同样的查找元素,如果都放在一起 ,不使用page object是能够通过link_text找到的,就是我分每个页面为一个对象时,这样就找不到元素了。是不是分开页面对象后 找不到当前页面是哪个了?
@sarua:
这种直接查找方式解决问题了,可以正常跑下去了。