Selenium click() - selects the button but doesn't click

Viewed 355

I'm using Selenium Python to do something like a Robotic Process Automation. However, I am facing problems clicking in a button... When I click the Search button manually nothing happens, but through Selenium the alert appears: enter image description here

The code I'm using is:

try:
    driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)').click()
except Exception as e:
    print(e)

The html section of the button is:

<fieldset class="PageBodyHorizontal" arbwidth="0" arbw="0,0,0,0" aropacity="1.0" arcolor="c0c0c0" arbcolor="#c0c0c0" style="width: 970px;">
   <legend class="hidden acc">Form Control Right Panel</legend>
   <div class="PageBody pbChrome" style="border-radius: 0px 0px 0px 0px ;-moz-border-radius: 0px 0px 0px 0px ;-webkit-border-radius: 0px 0px 0px 0px ;background: -moz-linear-gradient(top, rgba(192,192,192,1.0), rgba(192,192,192,1.0));background: -webkit-gradient(linear, center center, center center, from(rgba(192,192,192,1.0)),to(rgba(192,192,192,1.0)));background: linear-gradient(rgba(192,192,192,1.0), rgba(192,192,192,1.0));background-color:#c0c0c0;">
      <a href="javascript:" id="WIN_3_1002" arid="1002" artype="Control" ardbn="Query" artcolor="null" class="btn btn3d arfid1002 ardbnQuery" style="top: 5px; left: 10px; width: 50px; height: 21px; visibility: inherit; z-index: 997;" arwindowid="3">
         <div class="btntextdiv" style="top:0px; left:0px; width:50px; height:21px;">
            <div class="f1" style=";width:50px">Search</div>
         </div>
      </a>
   </div>
</fieldset>

enter image description here

It's strange because I have a similar code that works on other pages, for the same button.

The html of a similar button:

<fieldset class="PageBodyHorizontal" arbwidth="0" arbw="0,0,0,0" aropacity="1.0" arcolor="c0c0c0" arbcolor="#c0c0c0" style="width: 1654px;">
   <legend class="hidden acc">Panel2</legend>
   <div class="PageBody pbChrome" style="border-radius: 0px 0px 0px 0px ;-moz-border-radius: 0px 0px 0px 0px ;-webkit-border-radius: 0px 0px 0px 0px ;background: -moz-linear-gradient(top, rgba(192,192,192,1.0), rgba(192,192,192,1.0));background: -webkit-gradient(linear, center center, center center, from(rgba(192,192,192,1.0)),to(rgba(192,192,192,1.0)));background: linear-gradient(rgba(192,192,192,1.0), rgba(192,192,192,1.0));background-color:#c0c0c0;">
      <a href="javascript:" id="WIN_2_1000005683" arid="1000005683" artype="Control" ardbn="z3Btn Function Print Preview" artcolor="#" class="btn btn3d  btnd arfid1000005683 ardbnz3BtnFunctionPrintPreview" style="top:5px; left:149px; width:50px; height:21px;color:#;z-index:999;" arwindowid="2">
         <div class="btntextdiv" style="top:0px; left:0px; width:50px; height:21px;">
            <div class="f1" style=";width:50px">Print</div>
         </div>
      </a>
      <a href="javascript:" id="WIN_2_1002" arid="1002" artype="Control" ardbn="Query" artcolor="null" class="btn btn3d arfid1002 ardbnQuery" style="top: 5px; left: 10px; width: 50px; height: 21px; visibility: inherit; z-index: 997;" arwindowid="2">
         <div class="btntextdiv" style="top:0px; left:0px; width:50px; height:21px;">
            <div class="f1" style=";width:50px">Search</div>
         </div>
      </a>
      <a href="javascript:" id="WIN_2_303060100" arid="303060100" artype="Control" ardbn="z3Btn_NextStage" artcolor="null" class="btn btn3d arfid303060100 ardbnz3Btn_NextStage" style="top:5px; left:64px; width:82px; height:21px;z-index:998;" arwindowid="2">
         <div class="btntextdiv" style="top:0px; left:0px; width:82px; height:21px;">
            <div class="f7" style=";width:82px">Next Stage</div>
         </div>
      </a>
   </div>
</fieldset>

enter image description here

If you have advices on the quality of my code and how to fix this problem, I would be grateful.

2 Answers

Generally <div> tags are not interactable unless contenteditable="true" is set.

Some more details about the usecase would have helped us to analyze the observations in a canonical way. However to click on an element ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • Using CSS_SELECTOR I:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#WIN_3_1002 > div.btntextdiv > div.f1"))).click()
    
  • Using CSS_SELECTOR II:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn3d.ardbnQuery[artype='Control'][ardbn='Query']"))).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
    

Click on the a tag instead of div as the href to trigger some javascript code is on the a tag and not div.

      driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002').click()

Or try with action class

    elem = driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)')

    Webdriver.ActionChain(driver).move_to_element(elem).click()

try javascript executor:

    driver.execute_script("arguments[0].click()",elem)
Related