2

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.

Raphael
  • 57
  • 6

2 Answers2

1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

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)
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Thank you for your reply, I didn't knew that difference between tag and div... However I was unable to resolve the problem thanks to your suggestions. I believe the problem may be related with javascript or something written in the warning box. – Raphael Feb 17 '21 at 20:37
  • @Raphael do you have link to that website that we can try it out ? – PDHide Feb 17 '21 at 20:47
  • I'm sorry but no, you will not be able to access the site because it is through a vpn and you would need credentials. – Raphael Feb 17 '21 at 20:51
  • for both action chain and click you are getting that javascript error ? – PDHide Feb 17 '21 at 20:55
  • Yes, and as I said in the question, it is strange because on other pages the code that clicks the button works and if I click before the click() it also works ;(( – Raphael Feb 17 '21 at 20:59
  • can u add screenshot of that button ? and the inspect html you get – PDHide Feb 17 '21 at 21:05
  • if you devtools > console and run $(''#WIN_3_1002').click() and $('#WIN_3_1002 > div:nth-child(1)').click() what is happening – PDHide Feb 17 '21 at 21:28
  • if that works then use driver.execute_script("arguments[0].click()",elem) – PDHide Feb 17 '21 at 21:29
  • I didn't knew that :D But nothing happened, and in both cases on the console it printed long jsons – Raphael Feb 17 '21 at 21:40
  • I tried again executing your commands in the console, and both click in the button but the warning appears. – Raphael Feb 17 '21 at 21:52