gpt4 book ai didi

python - 如果我使用execute_script()函数,则无法使用Selenium截屏 - Selenium

转载 作者:行者123 更新时间:2023-11-30 22:46:55 24 4
gpt4 key购买 nike

我正在使用 Python Selenium 和 PhantomJS

我的目标是突出显示网页上所有可用表单的边框,然后截取屏幕截图。这就是我正在做的事情

    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

我正在获取

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 函数,则屏幕截图将成功保存。

我尝试在 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()返回多个元素 - 元素数组(实际上是 HTMLCollection) - 它没有 style 属性,这解释了 undefined 是不是对象错误。

通过索引获取一个元素:

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

或者,通过 selenium 定位元素,然后将其作为参数传递到脚本中:

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)

而且,这也可以扩展到多种形式:

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)

关于python - 如果我使用execute_script()函数,则无法使用Selenium截屏 - Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com