Selenium click() - 选择按钮但不点击

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

我正在使用 Selenium Python 来做类似机器人过程自动化的事情。但是,我在单击按钮时遇到问题...... 当我手动单击 Search 按钮时,没有任何反应,但通过 Selenium 出现警报:

我使用的代码是:

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

按钮的 html 部分是:

<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>

这很奇怪,因为我有一个类似的代码可以在其他页面上使用相同的按钮。

类似按钮的html:

<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>

如果您对我的代码质量以及如何解决此问题有任何建议,我将不胜感激。

通常 <div> 标签是 不可交互的,除非设置了

关于用例的更多细节将帮助我们以规范的方式分析观察结果。但是,要理想地单击一个元素,您需要引入 WebDriverWait for the and you can use the following :

  • 使用CSS_SELECTOR 我:

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

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn3d.ardbnQuery[artype='Control'][ardbn='Query']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

点击 a 标签而不是 div,因为触发一些 javascript 代码的 href 在 a 标签上而不是 div。

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

或尝试采取行动class

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

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

尝试 javascript 执行者:

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