Umair Umair - 14 days ago 10
Python Question

Cannot take screenshot with Selenium if I use execute_script() function - Selenium

I am using Python Selenium with PhantomJS

My goal is to highlight border of all Forms available on a webpage and then take screenshot. Here is what I am doing

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


I am getting

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


If I comment out
execute_script
function then screenshot gets successfully saved.

I tried to add delay between by
execute_script
and taking screenshot like

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


But same error.

Answer

getElementsByTagName() returns multiple elements - an array of elements (actually the HTMLCollection) - which does not have a style property which explains the undefined is not an object error.

Either get one element by index:

document.getElementsByTagName('form')[0].style['border'] = 'solid';  // ...

Or, locate the element via selenium and then pass it as an argument into the script:

form = self.driver.find_element_by_tag_name("form")
self.driver.execute_script("arguments[0].style['border'] = 'solid'; arguments[0].style['border-color'] = 'red';", form)

And, this scales up to multiple forms as well:

forms = self.driver.find_elements_by_tag_name("form")
for form in forms:
    self.driver.execute_script("""
        var form = arguments[0];
        form.style['border'] = 'solid'; 
        form.style['border-color'] = 'red';
    """, form)
Comments