0

I have a problem with finding element on a webpage. It's a popup window with a confirmation [NO]/[YES]. Selenium can find a parent of a buttons but not a button itself.

Popup window view

HTML block of code:

<div tabindex="-1" role="alertdialog" style="position: fixed; height: auto; width: 300px; top: 188.5px; left: 800px; max-width: 100%;" 
class="ui-dialog ui-dialog--notification ui-dialog--modern ui-widget ui-widget-content ui-front ui-dialog-buttons ui-draggable" 
aria-describedby="ui-id-3" aria-labelledby="ui-id-4">
 <div class="ui-dialog-titlebar ui-corner-all ui-widget-header ui-helper-clearfix ui-draggable-handle"></div>
    <div class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 32px;">
        <div class="a-AlertMessage">
         <div class="a-AlertMessage-body"> 
            <div class="a-AlertMessage-details" id="ui-id-3">Czy chcesz wygenerować pozycje dla całej dostawy?</div>
        </div>
    </div>
 </div>
 <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div 
    class="ui-dialog-buttonset">
         <button type="button" class="ui-button ui-corner-all ui-widget">Nie</button>
         <button type="button" class="js-confirmBtn ui-button ui-corner-all ui-widget ui-button--hot">Tak</button>
        </div>
    </div>
 </div>

Here is a code snippet that selenium can't find and gives an error 'Unable to locate element':

pop_up_yes_button = driver.find_element(By.XPATH, '//*[@id="t_PageBody"]/div[15]/div[3]/div/button[2]').click()

Parent is avaible for selenium without a problem:

driver.find_element(By.XPATH, '//*[@id="t_PageBody"]/div[15]')

I also tried WebDriverWait and ExpectedConditions but it gives TimeoutException:

WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, '//*[@id="t_PageBody"]/div[15]/div[3]/div/button[2]'))).click()
szumwuchu
  • 1
  • 2
  • Please share a link to that page or ALL the HTML as a text, not as a picture – Prophet Oct 28 '22 at 11:24
  • Sorry but i can't paste all of the HTML, page is in company's network. Edited and pasted part where the problem is – szumwuchu Oct 28 '22 at 12:25
  • The HTML you shared is not enough, not matching the XPath you using. There are several possible things that may cause the problem here. We can't say absolutely nothing based on what you shared. I'm sorry – Prophet Oct 28 '22 at 13:08

1 Answers1

0

As you said that it is a popup window, it is very likely that the button is in an iFrame. You have to wait until the iFrame loads and then search the element again.

Luckily, selenium has a built in function for dealing with iFrames:

your_button = WebDriverWait(driver, 
20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//*[@id="t_PageBody"]/div[15]/div[3]/div/button[2]')))
your_button.click()
  

Don't just copy and paste this code, understand this thing and implement it.

cigien
  • 57,834
  • 11
  • 73
  • 112