Frage

I have a website that generates a page via javascript using window.showmodaldialog. When using the Selenium IDE and firefox I was forced to monkeypatch the javascript to insert a new function to return the value from the dialog, convert window.showmodaldialog to window.open,

driver.execute_script("function setRetVal(mval) { window.myreturnval = mval;  }")
driver.execute_script("window.showModalDialog = function( sURL,vArguments, sFeatures) { if(window.myreturnval!=null) { var winarg = window.myreturnval; window.myreturnval = null; return winarg; } modalWin = window.open(sURL, vArguments, sFeatures); return false; }")

and modify onbeforeupload to send the result back.

driver.execute_script("window.onbeforeunload = function() { window.opener.setRetVal(window.returnValue); } ")

I'm now trying to convert to a python + selenium webdriver for testing purposes. I'd really like to get away from injecting new javascript during testing. The problem is that

driver.find_element_by_id("MainContent_buttonAccountLookup").click()

triggers this code

<a href="#" id="MainContent_buttonAccountLookup" 
    onclick="javascript:callLookup(&#39;
            Account&#39;,&#39;&lt;
            %=textBoxAccountName.ClientID %>&#39;,&#39;&lt;%=hiddenFieldAccountId.ClientID %>&#39;)
            ;return false;">
    <img alt="Account Lookup" src="../Images/search.GIF" border="0" />
</a>

which doesn't release control back to the python script until the window is closed.

How do I initiate a click so that I can get control of the new window, do my steps and close it?

War es hilfreich?

Lösung

You can switch to the new window after it is opened:

current_window = driver.current_window_handle
for handle in driver.window_handles:
    if handle != current_window:
        driver.switch_to_window(handle)

// perform actions
// close window

driver.switch_to_window(current_window)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top