无法使用python

时间:2016-08-02 11:35:29

标签: python selenium

我是selenium的新手,我想在弹出警报时拍摄警报框的屏幕截图。 我写的代码如下:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoAlertPresentException
import time


class SearchXSS(unittest.TestCase):

def setUp(self):
    self.driver = webdriver.Firefox()

def test_search_in_python_org(self):
    driver = self.driver
    driver.get("http://testfire.net/")
    ele = driver.find_element_by_name("txtSearch")
    ele.send_keys("<script>alert(document.cookie)</script>")
    ele.send_keys(Keys.RETURN)
    try:
        time.sleep(1)
        alert = driver.switch_to_alert()
        driver.get_screenshot_as_file('screenshot.png')
        alert.accept()
    except NoAlertPresentException as e:
        print "no alert to accept "
    fo.close()


def tearDown(self):
    self.driver.quit()

if __name__=="__main__":
    unittest.main()

我在拍摄屏幕截图时遇到了问题。错误信息如下

  

=============================================== =======================错误:test_search_in_python_org(主要 .SearchXSS)   -------------------------------------------------- -------------------- Traceback(最近一次调用最后一次):文件   &#34; /home/user/programs/sele/testx.py" ;,第22行,in   test_search_in_python_org       driver.get_screenshot_as_file(&#39; screenshot.png&#39;)文件&#34; /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",   第798行,在get_screenshot_as_file中       png = self.get_screenshot_as_png()File&#34; /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",   第817行,在get_screenshot_as_png中       return base64.b64decode(self.get_screenshot_as_base64()。encode(&#39; ascii&#39;))
  文件   &#34; /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py" ;,   第827行,在get_screenshot_as_base64中       return self.execute(Command.SCREENSHOT)[&#39; value&#39;] File&#34; /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py" ;,   第236行,执行中       self.error_handler.check_response(response)File&#34; /usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py",   第191行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪,值[&#39; alert&#39;]。get(&#39; text&#39;))UnexpectedAlertPresentException:Alert   文本:amSessionId = 92632533494消息:意外的模式对话框(文本:   amSessionId = 92632533494)Stacktrace:       在fxdriver.modals.closeUnhandledAlert /&lt; (文件:///tmp/tmpdnEJbt/extensions/fxdriver@googlecode.com/components/prompt-service.js:4745)       at(http://testfire.net/search.aspx?txtSearch=%3Cscript%3Ealert%28document.cookie%29%3C%2Fscript%3E:80

     

----------------------------------------------- -----------------------在10.346s中进行1次测试

     

失败(错误= 1)[以10.4s结束,退出代码为1]

帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

您无法使用screenshotalert,需要先处理alert acceptdismiss然后再截取屏幕截图selenium会截取当前提醒的屏幕截图,它总是会引发UnexpectedAlertPresentException使用alert无法使用selenium框进行屏幕截图。所以你需要做如下: -

alert = driver.switch_to_alert()
alert.accept()
driver.get_screenshot_as_file('screenshot.png')

如果你想使用alert截取屏幕截图,你应该尝试使用其他一些库的不同方法,在java中有Robot个类,可以在screenshot中使用Indexing events using cutoff of 5 Computing event counts... done. 3 events Dropped event B-NP:[w_2=bos, w_1=bos, w0=He, w1=reckons, w2=., w_1=bosw0=He, w0=Hew1=reckons, t_2=bos, t_1=bos, t0=PRP, t1=VBZ, t2=., t_2=bost_1=bos, t_1=bost0=PRP, t0=PRPt1=VBZ, t1=VBZt2=., t_2=bost_1=bost0=PRP, t_1=bost0=PRPt1=VBZ, t0=PRPt1=VBZt2=., p_2=bos, p_1=bos, p_2=bosp_1=bos, p_1=bost_2=bos, p_1=bost_1=bos, p_1=bost0=PRP, p_1=bost1=VBZ, p_1=bost2=., p_1=bost_2=bost_1=bos, p_1=bost_1=bost0=PRP, p_1=bost0=PRPt1=VBZ, p_1=bost1=VBZt2=., p_1=bost_2=bost_1=bost0=PRP, p_1=bost_1=bost0=PRPt1=VBZ, p_1=bost0=PRPt1=VBZt2=., p_1=bosw_2=bos, p_1=bosw_1=bos, p_1=bosw0=He, p_1=bosw1=reckons, p_1=bosw2=., p_1=bosw_1=bosw0=He, p_1=bosw0=Hew1=reckons] Dropped event B-VP:[w_2=bos, w_1=He, w0=reckons, w1=., w2=eos, w_1=Hew0=reckons, w0=reckonsw1=., t_2=bos, t_1=PRP, t0=VBZ, t1=., t2=eos, t_2=bost_1=PRP, t_1=PRPt0=VBZ, t0=VBZt1=., t1=.t2=eos, t_2=bost_1=PRPt0=VBZ, t_1=PRPt0=VBZt1=., t0=VBZt1=.t2=eos, p_2=bos, p_1=B-NP, p_2=bosp_1=B-NP, p_1=B-NPt_2=bos, p_1=B-NPt_1=PRP, p_1=B-NPt0=VBZ, p_1=B-NPt1=., p_1=B-NPt2=eos, p_1=B-NPt_2=bost_1=PRP, p_1=B-NPt_1=PRPt0=VBZ, p_1=B-NPt0=VBZt1=., p_1=B-NPt1=.t2=eos, p_1=B-NPt_2=bost_1=PRPt0=VBZ, p_1=B-NPt_1=PRPt0=VBZt1=., p_1=B-NPt0=VBZt1=.t2=eos, p_1=B-NPw_2=bos, p_1=B-NPw_1=He, p_1=B-NPw0=reckons, p_1=B-NPw1=., p_1=B-NPw2=eos, p_1=B-NPw_1=Hew0=reckons, p_1=B-NPw0=reckonsw1=.] Dropped event O:[w_2=He, w_1=reckons, w0=., w1=eos, w2=eos, w_1=reckonsw0=., w0=.w1=eos, t_2=PRP, t_1=VBZ, t0=., t1=eos, t2=eos, t_2=PRPt_1=VBZ, t_1=VBZt0=., t0=.t1=eos, t1=eost2=eos, t_2=PRPt_1=VBZt0=., t_1=VBZt0=.t1=eos, t0=.t1=eost2=eos, p_2B-NP, p_1=B-VP, p_2B-NPp_1=B-VP, p_1=B-VPt_2=PRP, p_1=B-VPt_1=VBZ, p_1=B-VPt0=., p_1=B-VPt1=eos, p_1=B-VPt2=eos, p_1=B-VPt_2=PRPt_1=VBZ, p_1=B-VPt_1=VBZt0=., p_1=B-VPt0=.t1=eos, p_1=B-VPt1=eost2=eos, p_1=B-VPt_2=PRPt_1=VBZt0=., p_1=B-VPt_1=VBZt0=.t1=eos, p_1=B-VPt0=.t1=eost2=eos, p_1=B-VPw_2=He, p_1=B-VPw_1=reckons, p_1=B-VPw0=., p_1=B-VPw1=eos, p_1=B-VPw2=eos, p_1=B-VPw_1=reckonsw0=., p_1=B-VPw0=.w1=eos] Indexing... done. Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at opennlp.tools.ml.model.AbstractDataIndexer.sortAndMerge(AbstractDataIndexer.java:89) at opennlp.tools.ml.model.TwoPassDataIndexer.<init>(TwoPassDataIndexer.java:105) at opennlp.tools.ml.AbstractEventTrainer.getDataIndexer(AbstractEventTrainer.java:74) at opennlp.tools.ml.AbstractEventTrainer.train(AbstractEventTrainer.java:91) at opennlp.tools.ml.model.TrainUtil.train(TrainUtil.java:53) at opennlp.tools.chunker.ChunkerME.train(ChunkerME.java:253) at com.oracle.crm.nlp.CustomChunker2.main(CustomChunker2.java:91) Sorting and merging events... Process exited with exit code 1. 方案的类型,但我不确定python中的等价物是什么。