2011-12-16 6 views
5

सेलेनियम 2 का उपयोग करना, वहाँ अगर एक तत्व बासी है परीक्षण करने के लिए एक तरीका है?सेलेनियम 2 का उपयोग कर एक बालों के तत्व की जांच करें?

मान लीजिए कि मैंने एक पृष्ठ से दूसरे पृष्ठ में एक संक्रमण शुरू किया है (ए -> बी)। मैं फिर तत्व एक्स का चयन करता हूं और इसका परीक्षण करता हूं। मान लीजिए कि तत्व एक्स ए और बी

अंततः एक्स को पृष्ठ संक्रमण से पहले ए से चुना जाता है और बी पर जाने के बाद परीक्षण नहीं किया जाता है, एक StaleElementReferenceException को बढ़ाता है। यह इस हालत के लिए जाँच करने के लिए आसान है:

try: 
    visit_B() 
    element = driver.find_element_by_id('X') # Whoops, we're still on A 
    element.click() 
except StaleElementReferenceException: 
    element = driver.find_element_by_id('X') # Now we're on B 
    element.click() 

लेकिन मैं नहीं बल्कि करना चाहते हैं:

element = driver.find_element_by_id('X') # Get the elment on A 
visit_B() 
WebDriverWait(element, 2).until(lambda element: is_stale(element)) 
element = driver.find_element_by_id('X') # Get element on B 

उत्तर

1

मैं आप किस भाषा का प्रयोग कर रहे हैं वहाँ पता नहीं है, लेकिन मूल विचार आप हल करने के लिए की जरूरत है यह है:

boolean found = false 
set implicit wait to 5 seconds 
loop while not found 
try 
    element.click() 
    found = true 
catch StaleElementReferenceException 
    print message 
    found = false 
    wait a few seconds 
end loop 
set implicit wait back to default 

नोट: बेशक, ज्यादातर लोग इसे इस तरह से नहीं करते हैं। समय अधिकांश लोग ExpectedConditions क्लास का उपयोग लेकिन ऐसे मामलों में जहां अपवाद इस विधि (मैं ऊपर राज्य) बेहतर काम कर सकते हैं बेहतर नियंत्रित किया जा करने के लिए की जरूरत है।

0

रूबी में,

$default_implicit_wait_timeout = 10 #seconds 

def element_stale?(element) 
    stale = nil # scope a boolean to return the staleness 

    # set implicit wait to zero so the method does not slow your script 
    $driver.manage.timeouts.implicit_wait = 0 

    begin ## 'begin' is Ruby's try 
    element.click 
    stale = false 
    rescue Selenium::WebDriver::Error::StaleElementReferenceError 
    stale = true 
    end 

    # reset the implicit wait timeout to its previous value 
    $driver.manage.timeouts.implicit_wait = $default_implicit_wait_timeout 

    return stale 
end 

कोड ऊपर ExpectedConditions द्वारा प्रदान की stalenessOf विधि का एक रूबी अनुवाद है। इसी कोड अजगर या किसी अन्य भाषा सेलेनियम का समर्थन करता है में लिखा जा सकता है, और तब तक तत्व बासी हो जाता है प्रतीक्षा करने के लिए एक WebDriverWait ब्लॉक से कहा जाता है।

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