I need to click on the Ok button on a page (HTML code snippet hereunder). The button itself does not have an Id and its XPath changes. It can be any of the following:
/html/body/div[3]/div[3]/div/button /html/body/div[4]/div[3]/div/button /html/body/div[5]/div[3]/div/button
The only stable locator I can find in the page is the noresultsfound Id of the div before the div that contains the button as a child div.
I am unable to guess which Selenium locator I could use to RELIABLY click on the button.
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-dialog-buttons ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="noresultsfound" aria-labelledby="ui-id-1" style="position: absolute; height: auto; width: 300px; top: 850px; left: 365px; display: block;"> <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span id="ui-id-1" class="ui-dialog-title">Attenzione</span><button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close" style="display: none;"><span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span><span class="ui-button-text">close</span></button></div> <div id="noresultsfound" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: auto;"> <p>Nessun risultato trovato</p> </div> <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"> <div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Ok</span></button></div> <p style="border-top: 1px solid #bcbcbc; margin-top:10px; padding-top:5px;color:#054a6c;font-style:italic;font-weight:bold;font-size:0.7em;margin-bottom: 0px;">Il portale รจ progettato per una visualizzazione ottimale con i seguenti browser:<br>Internet Explorer (V9), Chrome (V 35.0.1916.114m) e Firefox (V28 e29)</p> </div> <div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-w" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-sw" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-ne" style="z-index: 90;"></div> <div class="ui-resizable-handle ui-resizable-nw" style="z-index: 90;"></div> </div>
My code so far is like the following:
try: # things are good when the following error modal window is NOT found digram_notfound = DRIVER_WAIT.until( EC.visibility_of_element_located((By.ID, "noresultsfound")) ) if digram_notfound: click_xpath(driver, "/html/body/div[4]/div[3]/div/button") except TimeoutException: do useful stuff here
but as I said it fails on those pages where the XPath is different.
Is there a way of writing a locator anchored to the Id and find the button to click relative to that? Thanks
Advertisement
Answer
<div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Ok</span></button></div>
There are many selectors you could try for this element.
//button[./span[@class='ui-button-text' and text()='Ok']] //div[@class='ui-dialog-buttonset']/button