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.

2
  • Try element.submit() Commented Feb 17, 2021 at 19:47
  • Thank you for your reply, I didn't know about that method however replacing click() by submit(), I get "Message: Unable to locate element: ./ancestor-or-self::form" Commented Feb 17, 2021 at 19:59

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

10 Comments

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 do you have link to that website that we can try it out ?
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.
for both action chain and click you are getting that javascript error ?
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 ;((
|
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
    

3 Comments

Thank you for your reply, but I already tried with implicit and explicit waits, however the warning keeps appearing... I believe the problem may be related with javascript or something written in the warning box.
@Raphael Checkout the updated answer and let me know the status.
The warning also appeared ;((

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.