1
を私はPythonのセレンを使用していますPhantomJSではセレンとスクリーンショットを取ることができない - セレン
を私の目標は、Webページ上で使用可能なすべてのフォームの境界線を強調表示し、スクリーンショットを取ることです。ここで私は
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36")
self.driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any', '--web-security=false'])
self.driver.set_window_size(1024, 768)
self.driver.get(response.url)
self.driver.execute_script("document.getElementsByTagName('form').style['border'] = 'solid'; document.getElementsByTagName('form').style['border-color'] = 'red'")
self.driver.save_screenshot('test.png') # save a screenshot to disk
をやっているものです私はexecute_script
機能をコメントアウトした場合、スクリーンショットが正常に保存されます
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Scrapy-1.2.1-py2.7.egg/scrapy/utils/defer.py", line 102, in iter_errback
yield next(it)
File "/usr/local/lib/python2.7/dist-packages/Scrapy-1.2.1-py2.7.egg/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output
for x in result:
File "/usr/local/lib/python2.7/dist-packages/Scrapy-1.2.1-py2.7.egg/scrapy/spidermiddlewares/referer.py", line 22, in <genexpr>
return (_set_referer(r) for r in result or())
File "/usr/local/lib/python2.7/dist-packages/Scrapy-1.2.1-py2.7.egg/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr>
return (r for r in result or() if _filter(r))
File "/usr/local/lib/python2.7/dist-packages/Scrapy-1.2.1-py2.7.egg/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr>
return (r for r in result or() if _filter(r))
File "/home/mani/legibot/scrapy_app/spiders/spider.py", line 63, in parse_page
self.driver.execute_script("document.getElementsByTagName('form').style['border'] = 'solid'; document.getElementsByTagName('form').style['border-color'] = 'red'")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 465, in execute_script
'args': converted_args})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: {"errorMessage":"undefined is not an object (evaluating 'document.getElementsByTagName('form').style['border'] = 'solid'')","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"211","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55507","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"b09db570-b01b-11e6-b238-a5d1487ff8e7\", \"args\": [], \"script\": \"document.getElementsByTagName('form').style['border'] = 'solid'; document.getElementsByTagName('form').style['border-color'] = 'red'\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/b09db570-b01b-11e6-b238-a5d1487ff8e7/execute"}}
Screenshot: available via screen
を取得しています。
私はexecute_script
と
self.driver.execute_script("document.getElementsByTagName('form').style['border'] = 'solid'; document.getElementsByTagName('form').style['border-color'] = 'red'")
time.sleep(2)
self.driver.save_screenshot('test.png') # save a screenshot to disk
しかし、同じエラーのようにスクリーンショットを取ることによって、間の遅延を追加しようとしました。
getElementsByTagName()
私がこの質問を書いていたとき、あなたが@alecxeが私の質問に答えるなら、私は祈っていました。 2分で...ありがとう、百万...あなたは私に多くの時間を節約しました – Umair
@おかげでもう一度面白いとよく説明された質問のUmair。これが問題の解決に役立つことを願っています。 – alecxe
ええ、それは解決しました...私はJSコードで間違いかもしれないと思っていません... – Umair