Search code examples
seleniumtestingselenium-webdriverselenium-chromedriverweb-testing

Selenium Driver is one page behind ajax call after large json data download


I have a code like this

driver.find_element_by_xpath("//div[@aria-rowindex=7][@aria-colindex=10]//child::span[1]").click() #click will take me to a new ajax page - no change in url
driver.implicitly_wait(60)
time.sleep(160) #without this the below a.png is still behind before my previous click
driver.save_screenshot('a.png') #new url page is in screenshot properly. 
#working fine until this line
driver.implicitly_wait(60)
print(driver.page_source) #still in old page's page_source and the below xpath is not available here
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//button[@button_id='fuseBlowView']"))).click()

Why is the driver still with my old page even if screenshot is showing new page? Now it is erroring out like this:

    WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//button[@button_id='fuseBlowView']"))).click()
  File "C:\Users\jrex\PycharmProjects\Dragon\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

It is not able to find the xpath, because the old page url is still in driver.page_source In web browser which selenium opens, I am even able to click button during the wait time - I simply tried - I did a screenshot just to prove that. But why is my driver lagging behind?

There is a lot of data comes in after the 1st click to the a.png That is why wait time. Just to bring all the data in. I am using Google Chrome Driver.


Solution

  • Try getting the number of iframes in your webpage by iframes = driver.find_elements_by_tag_name("iframe") and then switch to a corresponding iframe and find that desired element and click. It might help!