折腾:
【未解决】Mac中Selenium换用PhantomJS去模拟浏览器操作
期间,把WebDriver从chrome换成PhantomJS后,结果代码:
loginElement = driver.find_element_by_id(“ctl00_hdr_hpi_aLogin”)
logging.debug(“loginElement=%s”, loginElement)
logging.info(“Found Login button”)
loginElement.click()
click出错:
loginElement.click()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py”, line 80, in click
self._execute(Command.CLICK_ELEMENT)
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py”, line 501, in _execute
return self._parent.execute(command, params)
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 308, in execute
self.error_handler.check_response(response)
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py”, line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {“errorMessage”:”Element is not currently visible and may not be manipulated”,”request”:{“headers”:{“Accept”:”application/json”,”Accept-Encoding”:”identity”,”Connection”:”close”,”Content-Length”:”81″,”Content-Type”:”application/json;charset=UTF-8″,”Host”:”127.0.0.1:54141″,”User-Agent”:”Python http auth”},”httpVersion”:”1.1″,”method”:”POST”,”post”:”{\”sessionId\”: \”23f88e40-cf98-11e7-bb37-9dba8cfd1865\”, \”id\”: \”:wdc:1511363890673\”}”,”url”:”/click”,”urlParsed”:{“anchor”:””,”query”:””,”file”:”click”,”directory”:”/”,”path”:”/click”,”relative”:”/click”,”port”:””,”host”:””,”password”:””,”user”:””,”userInfo”:””,”authority”:””,”protocol”:””,”source”:”/click”,”queryKey”:{},”chunks”:[“click”]},”urlOriginal”:”/session/23f88e40-cf98-11e7-bb37-9dba8cfd1865/element/:wdc:1511363890673/click”}}
Screenshot: available via screen
很明显,是元素不可见
重新再去运行一遍,排除是否是网络影响
此处由于PhantomJS没有界面,导致也没法看到出错时,页面的具体效果是啥
selenium phantomjs ElementNotVisibleException
试试最大化窗口
driver.maximize_window()
python – Selenium on mac gives elementNotVisibleException – Stack Overflow
Python – Selenium: Element is not currently visible and may not be manipulated – Stack Overflow
果然用了最大化窗口,就可以了。
【总结】
PhantomJS默认虽然的确使用了窗口,但是默认窗口很小,可以通过截图看到
下图是放大很多很多后才能看清内容:
就像是移动端的页面
导致很多元素默认是不可见。
然后加上:
driver.maximize_window()
后,截图如下:
(下面是没有放大,就可以看到屏幕宽度是宽很多了)
从而很多元素可见了。就解决了此处的问题。
转载请注明:在路上 » 【已解决】Selenium中PhantomJS出错:selenium.common.exceptions.ElementNotVisibleException Message errorMessage Element is not currently visible and may not be manipulated