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 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 ><span id="ui-id-1" >Attenzione</span><button role="button" aria-disabled="false" title="close" style="display: none;"><span ></span><span >close</span></button></div>
<div id="noresultsfound" style="width: auto; min-height: 0px; max-height: none; height: auto;">
<p>Nessun risultato trovato</p>
</div>
<div >
<div ><button type="button" role="button" aria-disabled="false"><span >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 style="z-index: 90;"></div>
<div style="z-index: 90;"></div>
<div style="z-index: 90;"></div>
<div style="z-index: 90;"></div>
<div style="z-index: 90;"></div>
<div style="z-index: 90;"></div>
<div style="z-index: 90;"></div>
<div 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
CodePudding user response:
<div ><button type="button" role="button" aria-disabled="false"><span >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
CodePudding user response:
You may try these:
//button[@type='button']
CodePudding user response:
The element you are looking for can be simply located by this XPath:
"//div[@class='ui-dialog-buttonset']//button"
So your code could be something like this:
try:
digram_notfound = DRIVER_WAIT.until(EC.visibility_of_element_located((By.ID, "noresultsfound")))
if digram_notfound:
click_xpath(driver, "//div[@class='ui-dialog-buttonset']//button")
except TimeoutException:
#do useful stuff here
