0

What I need to do here is to click an OK button in a pop up. That OK button is mentioned in the inspect menu as: id="btn-confirm-yes". Used this id to access the element, tried to copy its xpath and access via that and tried to access it via the div class. Unfortunately, everything returned element not found. Its hard for me to see why.

 #This is the python script used to access 
 element = driver.find_element_by_id("btn-confirm-yes")  
 if element.is_displayed():  
   print "Confirm button element is there"
   driver.implicitly_wait(4) # seconds
   element.send_keys(Keys.RETURN)
 else:
   print "Confirm button element not found" 


 /* Hierarchy from Inspect menu */
 <div id="sing-out-confirm" class="reveal-modal small open" style="top: 100px; opacity: 1; visibility: visible; display: block;">
<h2 class="text-uppercase">ALL DEVICE SIGNOUT</h2>
<p>Click OK to confirm All Device Signout.</p>
<form id="sign-out-user-account-form" method="post" action="/portal-owner-web/user-account/sign-out-all-device">
    <div class="button-bar">
        <a class="button small close-reveal-modal mr5" href="#">Cancel</a>

        <a class="button small reveal-modal-button mr5" href="#" id="btn-confirm-yes">OK</a>
    </div>
</form>
</div

Need to access the element which is specified in the inspect window as: class="button small reveal-modal-button mr5" href="#" id="btn-confirm-yes"

A nudge in the right direction would be very helpful!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0
  1. Make sure that the button, you're trying to detect does not belong to the inline frame
  2. It might be the case the button is not immediately available in DOM immediately, for example it is being added after the main content as the result of an AJAX call. Therefore a good practice is to use Explicit Wait in order to let Selenium to poll the DOM periodically for element appearance.

Reference code:

element = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "btn-confirm-yes")))

You will also need to add the following import statements:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Suggestion 2. did not work. I am not even sure if its waiting those 10 seconds for the polling. I will check regarding suggestion 1. – Shoevo205 Jul 07 '19 at 12:27
  • A version of suggestion 2 worked, in the end. The element was not being loaded on time when I was checking for it. So added a sleep(10) in place of the explicit wait, and it worked. – Shoevo205 Jul 07 '19 at 18:27
0

It is pretty much evident from the HTML that the element is within a Modal Dialog. So to click() on the OK button you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "OK"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.small.reveal-modal-button.mr5#btn-confirm-yes"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button small reveal-modal-button mr5' and @id='btn-confirm-yes'][text()='OK']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352