2

I am clicking a link which takes me to new page, but I don't know how to proceed after that. When I try to find an element by id in the next page it gives me error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"myid"} 

My code:

browser = webdriver.Firefox()

browser.get('myurl')
elem = browser.find_element_by_id('closeMessageButton') # Find the search box
elem.click() # this takes me to new link

# now I am searching id from new page
elem = browser.find_element_by_id('newpage_id')  #
elem.click()

I have seen this http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html but couldn't succeed in implementing for my case. I think the information in browser is not updated yet.

  • 1
    page? As in new browser tab or new window? You would need to switch to new window or tab to recognize that new element. – demouser123 Jul 14 '15 at 2:41
  • Also you would need to implement some kind of wait, so that the script waits for some time, before jumping into new page and then finding element. – demouser123 Jul 14 '15 at 2:42
3

The problem is that you don't switch to opened window, and webdriver searches for elements in the old page instead of the newly opened one.

Solution:

String winHandleBefore = driver.getWindowHandle();
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }

This java method's part switches to the newest window. Also consider maximizing it, because sometimes tests masses up in not maximized windows. Another point: waiting. Try to read here about different types of wait. Good luck.

  • thank you ,this is useful for me may i know how to write this sample code – user41695 Sep 26 at 13:29
0

Solution for Python. This code will switch to the second window in your browser.

window_after = driver.window_handles[1]
driver.switch_to.window(window_after)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.