简体   繁体   English

如何摆脱硒中的特定异常?

[英]How to get rid of specific exceptions in selenium?

I have to use the selenium for my project.when i am doing using selenium.i am giving the at each step like this 我必须在项目中使用硒。当我使用硒时,我会在每个步骤中都这样给出

   time.sleep(6)

i am giving the sufficient interval for loading particular page, but still I am facing the below error at somepoint. 我为加载特定页面提供了足够的时间间隔,但仍然在某些时候遇到以下错误。 Mostly i am getting the exceptions like 大多数情况下,我会遇到类似的异常情况

Regular Exceptions: 常规例外:

   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)

I am also getting No Such Implementation Error because of WebDriverWait().Until(): 由于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 

What is the way to get rid of such exceptions? 摆脱此类异常的方法是什么?

Could be 2 things. 可能是两件事。

  1. Your element hasn't finished loading within the allocated 6 seconds 您的元素在分配的6秒钟内尚未完成加载
  2. Your locator is malformed. 您的定位器格式不正确。 This looks suspect ... 这看起来很可疑...

    { "method":"xpath","selector":"//textarea[@style=\\"overflow-y: hidden; {“方法”:“ xpath”,“选择器”:“ // textarea [@style = \\” overflow-y:隐藏; height: 50px; 高度:50px; width: 300px;\\"]"}' 宽度:300像素; \\“]”}'

Can you check that the locator returns what you expect using FireFinder or similar? 您能否使用FireFinder或类似工具检查定位器是否返回了您期望的结果?

If the locator is ok, you could try using WebDriverWait & ExpectedCondition to confirm the page has finished loading rather than gambling that you page will always load in 6s 如果定位器正常,则可以尝试使用WebDriverWait&ExpectedCondition确认页面已完成加载,而不是赌博说页面将始终在6秒内加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM