2013-07-08 6 views
27

सेलेनियम पायथन वेबड्राइवर में कमांड निष्पादन विलंबता के लिए अधिकतम समय सीमा निर्धारित करने का एक अच्छा तरीका खोजने का प्रयास कर रहा है। आदर्श रूप से, कुछ:सेलेनियम पायथन वेबड्राइवर डिफ़ॉल्ट टाइमआउट कैसे सेट करें?

my_driver = get_my_driver() 
my_driver.set_timeout(30) # seconds 
my_driver.get('http://www.example.com') # stops/throws exception when time is over 30  seconds 

काम करेगा। मुझे .implicitly_wait(30) मिला है, लेकिन मुझे यकीन नहीं है कि यह वांछित व्यवहार में परिणाम देता है या नहीं।

यदि यह उपयोगी है, तो हम विशेष रूप से फ़ायरफ़ॉक्स के लिए वेबड्राइवर का उपयोग कर रहे हैं।

संपादित

@ अमय के जवाब के अनुसार, इस उपयोगी हो सकता है:

ff = webdriver.Firefox() 
ff.implicitly_wait(10) # seconds 
ff.get("http://somedomain/url_that_delays_loading") 
myDynamicElement = ff.find_element_by_id("myDynamicElement") 

हालांकि, यह मेरे लिए स्पष्ट नहीं है निहित प्रतीक्षा दोनों get पर लागू होता है कि क्या (जो वांछित है कार्यक्षमता) और find_element_by_id पर।

बहुत बहुत धन्यवाद!

+1

मैं स्रोत कोड पर एक नज़र था। यह अजगर बाध्यकारी के लिए अस्पष्ट है। लेकिन सी # के लिए, 'ImplicitlyWait' केवल' FindElement/FindElements' (जावा के लिए समान) के लिए काम करता है। स्रोत: [1] (https://code.google.com/p/selenium/source/browse/dotnet/src/WebDriver/ITimeouts.cs#48) [2] (https://code.google.com/ पी/सेलेनियम/मुद्दों/विस्तार? आईडी = 50 9 2) –

+0

धन्यवाद। यदि आप रुचि रखते हैं तो नीचे मेरा उत्तर देखें। –

उत्तर

64

अजगर में, एक पृष्ठ लोड करने के लिए समय समाप्त बनाने के लिए विधि है: जब भी पेज लोड अधिक से अधिक 30 सेकंड लेता है

driver.set_page_load_timeout(30) 

यह एक TimeoutException फेंक देते हैं।

+2

यह क्रोम ड्राइवर के साथ काम नहीं करता है। – sorin

+1

अधिक जानकारी हासिल करने या संपादित करने की देखभाल? –

4

स्पष्टीकरण और लागू प्रतीक्षा के बारे में जानकारी here मिल सकती है।

अद्यतन

जावा मैं यह देख, this के आधार में:

WebDriver.Timeouts pageLoadTimeout(long time, 
           java.util.concurrent.TimeUnit unit) 

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite. 

Parameters: 
    time - The timeout value. 
    unit - The unit of time. 

अजगर बराबर के बारे में सुनिश्चित नहीं है।

3

सबसे अच्छा तरीका है स्थापित करने के लिए है प्राथमिकता:

fp = webdriver.FirefoxProfile() 
fp.set_preference("http.response.timeout", 5) 
fp.set_preference("dom.max_script_run_time", 5) 
driver = webdriver.Firefox(firefox_profile=fp) 

driver.get("http://www.google.com/") 
+0

क्या यह किसी प्रकार की त्रुटि लौटाता है? –

+0

@ivan_bilan: यदि आपका मतलब 'एक्सपशन' है, नहीं, तो यह किसी भी –

+0

'dom.max_script_run_time' को जावास्क्रिप्ट निष्पादित करने के लिए टाइमआउट सेट नहीं करता है। यह एक पूर्ण pageload टाइमआउट नहीं है। –

1

मेरे समाधान ब्राउज़र लोड ईवेंट के साथ एक अतुल्यकालिक धागा चलाने के लिए, और यह ब्राउज़र बंद करने और लोड समारोह को फिर से फोन करता है, तो वहाँ था के लिए था एक समय समाप्त।

#Thread 
def f(): 
    loadStatus = true 
    print "f started" 
    time.sleep(90) 
    print "f finished" 
    if loadStatus is true: 
     print "timeout" 
     browser.close() 
     call() 

#Function to load 
def call(): 
    try: 
     threading.Thread(target=f).start() 
     browser.get("http://website.com") 
     browser.delete_all_cookies() 
     loadStatus = false 
    except: 
     print "Connection Error" 
     browser.close() 
     call() 

कॉल() एक समारोह जो सिर्फ

संबंधित मुद्दे