2016-11-19 72 views
4

我在Selenium的這個ElementNotVisibleException問題上看到了很多問題/回答。 到目前爲止,我已經做了最好的代碼如下: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是由司機下車,但它不可能發送任何密鑰。 還有這裏的答案:哪些建議使用ActionChain Selenium Element not visible exception

,但我想沒有成功:(

如果我做的:

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... 

回答

0

除了「.invisibility_of_element_located」,您可以嘗試「visibility_of_element_located」。

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") 
相關問題