Search code examples
javascriptpythonseleniumwait

Python Selenium : is execute_script able to wait for javascript content to load?


I am trying to automate a simple action with python and selenium (with safari) : I have a webpage with a list on top of it going from "A" to "Z", each letter activate a Javascript on the page (at least I guess, i don't know really well Javascript and HTML) when you click on it, making a table below the letter list to reload and output a list of stocks ("A" give you the stocks starting with letter A, "B" all the stocks starting with letter B, etc...) My goal is to click on each button, list all the stocks of the table, store the list in Python and then go to next letter. So I created a method that clicks on the letter passed as an argument, but I want python to wait for the table to 'load' before allowing it to go to the next line of my code because for now it is creating problems as the script goes on the next letter before the table is loaded.

The command I use to click on the buttons is :

driver.execute_script("arguments[0].click();",driver.find_element_by_xpath(f"//li[text()='{Stock_Letter.upper()}']"))

where driver is my selenium webdriver, and Stock_Letter is the method's argument containing the element i want to click To loop all the letters I use :

    alphaB = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

    for lettre in alphaB:
        Session.GoToGamePage(page='buysell', Stock_Letter=lettre.upper())

(Session is my Object, and GoToGamePage is the method I use to click the expected letter, it contains other stuffs game related)

I tried to use the wait.until method of selenium but the problem is that the first element of the table has the same HTML attributes no matter the letter, so unless it exists a selenium expected condition to wait for special text, I don't know how to do it ? Or even an expected condition to wait for the first letter of an attribute to be some value, but I can't find a complete documentation on expected conditions ?? My first guess would be to code the waiting in the execute_script command but I really don't know how to do it ? If you need any further information on the webpage itself let me know, here is the first table row data :

<td class="allf"><a href="display-EDENp.aspx">EDENRED</a></td>

The webpage concerned is this one : https://school.abcbourse.com/skemafinance/tab_market.aspx

but you need to create an account (really quick) on https://school.abcbourse.com/skemafinance and use the code 195 (feel free to participate in the trading contest if you want to !)


Solution

  • To answer your first question -- no, execute_script does not do any waiting. It just runs some Javascript on the page, and that's it. You mentioned that the first element in your table has the same HTML attributes as all the others, so you can't just wait on that element. That makes sense.

    You could try waiting on the presence of ALL elements located by your selector and see if that helps.

    WebDriverWait(driver, 10).until(
            EC.presence_of_all_elements_located((By.XPATH, "//li[text()='{Stock_Letter.upper()}']")))
    

    You can also use text_to_be_present_in_element to wait on element.Text to equal something, or you can wait on text_to_be_present_in_element_value to wait on an element's value attribute to equal something, depending on what the HTML looks like.

    This documentation may help:

    https://selenium-python.readthedocs.io/waits.html