Selenium - ElementNotVisibleException

时间:2016-11-19 22:48:47

标签: python selenium automated-tests

我在selenium的这个ElementNotVisibleException问题上看到了很多问题/答案。 我到目前为止所做的最好的代码如下:

from selenium.webdriver.support.ui import WebDriverWait
from selenium                      import webdriver
from selenium.webdriver.common.by  import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support    import expected_conditions as EC
url      = "http://www.cfnews.net/user/login"
driver.get(url)
print "Got url."
wait = WebDriverWait(driver, 10)
login_form = wait.until(EC.invisibility_of_element_located((By.ID, "user-login")), "pbm")
print "Element located."
displayed = login_form.is_displayed()
print "login_form.is_displayed() = ", displayed
login_form.send_keys("a")

给出以下输出:

Got url.
Element located.
login_form.is_displayed() =  False
---------------------------------------------------------------------------
ElementNotVisibleException                Traceback (most recent call last)
<ipython-input-110-d45e06b37320> in <module>()
     12 displayed = login_form.is_displayed()
     13 print "login_form.is_displayed() = ", displayed
---> 14 login_form.send_keys("a")

/Users/romain/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.pyc in send_keys(self, *value)
    345                 value = self._upload(local_file)
    346 
--> 347         self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
    348 
    349     # RenderedWebElement Items

/Users/romain/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.pyc in _execute(self, command, params)
    492             params = {}
    493         params['id'] = self._id
--> 494         return self._parent.execute(command, params)
    495 
    496     def find_element(self, by=By.ID, value=None):

/Users/romain/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
    234         response = self.command_executor.execute(driver_command, params)
    235         if response:
--> 236             self.error_handler.check_response(response)
    237             response['value'] = self._unwrap_value(
    238                 response.get('value', None))

/Users/romain/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.pyc in check_response(self, response)
    190         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    191             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 192         raise exception_class(message, screen, stacktrace)
    193 
    194     def _value_or_default(self, obj, key, default):

ElementNotVisibleException: Message: element not visible
  (Session info: chrome=54.0.2840.98)
  (Driver info: chromedriver=2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1),platform=Mac OS X 10.12.0 x86_64)

因此,页面被下载,html由驱动程序获取,但是无法发送任何密钥。 这里也有答案: Selenium Element not visible exception

建议使用ActionChain,但我尝试没有成功:(

如果我这样做:

driver.page_source

我得到了html:

u'<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"><head>\n        \n                                                                                    \n<title>Ouverture de session / Utilisateur - l\'actualit\xe9 du capital investissement : transactions LBO, M&amp;A, Venture France - Corporate Finance et Private Equity</title>\n<meta name="description" content="CFNEWS est le premier site \xe0 proposer actualit\xe9s et r\xe9f\xe9rentiels du corporate finance en France sur les...etc...

2 个答案:

答案 0 :(得分:2)

试试这个并成功!!!

from selenium.webdriver.support.ui import WebDriverWait
from selenium                      import webdriver
from selenium.webdriver.common.by  import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support    import expected_conditions as EC

driver = webdriver.Firefox()
url      = "http://www.cfnews.net/user/login"
driver.get(url)

wait = WebDriverWait(driver, 10)
login_form = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#nom-user")))

login_form.send_keys("a")

答案 1 :(得分:0)

而不是&#34; .invisibility_of_element_located&#34;你可以试试&#34; visibility_of_element_located&#34;。