【问题标题】:How to get rid of specific exceptions in selenium?如何摆脱硒中的特定异常?
【发布时间】:2012-02-01 15:39:38
【问题描述】:

我必须在我的项目中使用 selenium。当我在使用 selenium 时。我在每一步都像这样

   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/fxdriver@googlecode.com/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().Until(),我也没有收到此类实施错误:

    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/fxdriver@googlecode.com/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 

有什么方法可以消除这种异常?

【问题讨论】:

    标签: python selenium webdriver timeoutexception


    【解决方案1】:

    可能是两件事。

    1. 您的元素尚未在分配的 6 秒内完成加载
    2. 您的定位器格式错误。这看起来很可疑......

      { "method":"xpath","selector":"//textarea[@style=\"overflow-y: hidden;高度:50px;宽度:300px;\"]"}'

    您能否使用 FireFinder 或类似工具检查定位器是否返回您期望的结果?

    如果定位器没问题,您可以尝试使用 WebDriverWait & ExpectedCondition 来确认页面已完成加载,而不是赌您的页面将始终在 6 秒内加载

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2020-11-03
      • 2010-12-19
      • 2020-01-27
      • 2014-01-10
      • 1970-01-01
      相关资源
      最近更新 更多