phindle
phindle

Reputation: 73

Selenium can't find element even after explicit waiting in an Ajax Page

I'm trying to automate updating fields in a web application. Thus the url does not change after logging in

Here is my code so far

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

path_to_chromedriver = "C:/chromedriver"
browser = webdriver.Chrome(executable_path=path_to_chromedriver)

"""Login Page"""
login_url = "url"
browser.get(login_url)

username = browser.find_element_by_id("username")
password = browser.find_element_by_id("password")

username.send_keys("username")
password.send_keys("password")

browser.find_element_by_name("submit").click()

"""Application front page"""
searchBar = browser.find_element_by_id("searchBar")
searchBar.send_keys("item to be searched")

button = browser.find_element_by_id("searchButton")
button.click()

"""Click on item on search results"""
#starting here, everything doesn't work
wait = WebDriverWait(browser,10)
item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#item')))
item.click()#this never works as it just times out

This site is a web application. I've printed out the page source after each click and it doesn't change after the homepage however in the Chrome browser it does change. Explicit and Implicit waits both do not work. Any suggestions?

Thanks in advance

--edit--

I'm a bit hesitant to post the html because it's a custom web app. However, the class of the body is "dhtmlx_winviewport" and the part of the web app that did change starts out with something like

<iframe id = "frameID" name = "frame1" src="some link that shows the item I searched for" height="400" width="400" frameboorder="0" style="z-index: 10; position: absolute; visibility: visible; width:400px; height:800px;"> == $0

The thing I want to click on is a cell in a table

<td align="left" valign="middle" title="title">title</td>

The error I am getting is

Traceback (most recent call last): File "C:\script.py", line 45, in item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'css'))) File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

Upvotes: 2

Views: 1140

Answers (2)

Trevor K
Trevor K

Reputation: 125

Where is your object instantiation for Wait, is it not in this block of code? For example mine is -

self._wait = WebDriverWait(driver, 15)

You might also try using 'element to be clickable' rather than 'presence of element located'

item = self._wait.until(EC.element_to_be_clickable((By.XPATH,"//MY_XPATH_IS_HERE")))

Are you getting any error messages? Does item==None return true?

Upvotes: 0

Ywapom
Ywapom

Reputation: 603

If your the app uses JQuery for the Ajax request you can check for JQuery.active is zero before continuing. Something like:

count = 0
while(count < max_secs):
    count += 1
    is_ajax_done = driver.execute_script("return window.JQuery != undefined && JQuery.active == 0")
    if is_ajax_done:
        # continue
    else:
        time.sleep(1)

Upvotes: 0

Related Questions