gallly
gallly

Reputation: 1211

webdriver 'Element not found in the cache, perhaps page has changed' [webdriver][python]

I have created a test to create a ticket. When I click 'new ticket', it brings up a popup form. I fill in the title, and body, and click 'save and new'. At this point I created a loop. While i<10, repeat the test. I can get to the first ticket, but during the second ticket, it just exits out and says the error in my title. I have gotten to the point where it almost starts the 2nd ticket, but it enters the text in the wrong area, which I don't understand. It enters text in the field one above my 'title'. I have the send_keys based on the ID as well. here is the code:

while i<2:  
            driver.implicitly_wait(10)
            title = f.name()
            driver.find_element_by_name('txtsummary')
            driver.find_element_by_name('txtsummary').send_keys(title)
            driver.find_element_by_name('txtcontactorg').send_keys('unnamed')
            body = f.name()       
            page = driver.find_elements_by_xpath('//td[@class="mceIframeContainer mceFirst mceLast"]/iframe')[1]
            page.click()
            page.send_keys(body)           
            driver.find_element_by_xpath('//span[text()="Save and New"]').click()   

If I take out the implicitly wait, it enters text right after saving the first one, into the wrong box. If I leave it in, it just waits 10 seconds and closes out. My first ticket gets created always, and it always takes forever than fails for the next ticket. I mean that in my last line of code, the save and new, it takes like 8 seconds for that line to run, when it should be instantly after sending body. I have a feeling this is a factor.

Upvotes: 0

Views: 5241

Answers (1)

TehTris
TehTris

Reputation: 3217

I believe the problem has to do with the fact that you are selecting an iframe. Once you are in the iframe, it is unable to re-find 'txtsummary'.

The reason this happens is because the focus has been changed to a place where 'txtsummary' doesnt exist as soon as you clicked the iframe. To return focus to the topmost frame insert.

driver.switch_to_default_content()

at the top of your loop, and every time that your loop is run, it will start at the top most frame.

Upvotes: 4

Related Questions