2012-02-01 81 views
0

我必須用我project.when硒我使用selenium.i我在這樣如何擺脫硒中的特殊例外情況?

time.sleep(6) 

我給加載特定頁面足夠的間隔每一步給人做,但還是我在某個點面對下面的錯誤。 晴我得到異常喜歡

定期例外:

Exception: Message: u'Unable to locate element: {"method":"xpath","selector":"//textarea[@style=\\"overflow-y: hidden; height: 50px; width: 300px;\\"]"}' ; Stacktrace: Method WebDriverError threw an error in file:///tmp/tmpBxGp2P/extensions/[email protected]/resource/modules/atoms.js 
    Traceback (most recent call last): 
    File "crawl_optimize.py", line 206, in main 
    word_query = browser.find_element_by_xpath('//textarea[@style="overflow-y: hidden; height: 50px; width: 300px;"]') 
    File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 210, in find_element_by_xpath 
    return self.find_element(by=By.XPATH, value=xpath) 
    File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 643, in find_element 
    {'using': by, 'value': value})['value'] 
    File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 153, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/errorhandler.py", l  ine 147, in check_response 
    raise exception_class(message, screen, stacktrace) 

,我也越來越因爲WebDriverWait()沒有這樣的執行錯誤,直到():

NoSuchElementException: Message: u'Unable to locate element: { "method":"xpath","selector":"//textarea[@style=\\"overflow-y: hidden; height: 50px; width: 300px;\\"]"}' ; Stacktrace: Method WebDriverError threw an error in file:///tmp/tmpBxGp2P/extensions/[email protected]/resource/modules/atoms.js 
    keyword : balance transfer ad pos : 2 
    Exception: Message: None 
    Traceback (most recent call last): 
    File "crawl_optimize.py", line 228, in main 
    start_cpc,end_cpc = set_initial_cpc(initial_max_cpc,ad_position) 
    File "crawl_optimize.py", line 95, in set_initial_cpc 
    ep = crawl_position(cost) 
    File "crawl_optimize.py", line 82, in crawl_position 
    WebDriverWait(browser, 10).until(lambda driver : driver.find_element_by_link_text(word.lower())) 
    File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/support/wait.py", line 55, in until 
    raise TimeoutException() 
    TimeoutException: Message: None 

什麼擺脫這種例外的方式?

回答

0

可能是2件事。

  1. 你的元素尚未分配的6秒
  2. 你的定位是畸形內完成加載。這看起來可疑......

    {「method」:「xpath」,「selector」:「// textarea [@style = \」overflow-y:hidden; height:50px; \「]」}'

您可以檢查定位器是否可以使用FireFinder或類似方法返回您期望的結果嗎?

如果定位是好的,你可以嘗試使用WebDriverWait & ExpectedCondition確認頁面加載完成,而不是賭博,你頁面會一直在6S

加載