Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium click()-选择按钮,但不';点击_Python_Python 3.x_Selenium_Selenium Webdriver_Firefox - Fatal编程技术网

Python Selenium click()-选择按钮,但不';点击

Python Selenium click()-选择按钮,但不';点击,python,python-3.x,selenium,selenium-webdriver,firefox,Python,Python 3.x,Selenium,Selenium Webdriver,Firefox,我正在使用Selenium Python做一些类似机器人过程自动化的事情。但是,我在点击一个按钮时遇到了问题。。。 当我手动单击“搜索”按钮时,不会发生任何事情,但通过Selenium会显示警报: 我使用的代码是: try: driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)').click() except Exception as e: print(e) 按钮的html部分是: &

我正在使用Selenium Python做一些类似机器人过程自动化的事情。但是,我在点击一个按钮时遇到了问题。。。 当我手动单击“搜索”按钮时,不会发生任何事情,但通过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>

小组2

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

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

关于用例的更多细节将有助于我们以规范的方式分析观察结果。但是,理想情况下,要单击某个元素,您需要对进行归纳,并且可以使用以下方法:

  • 使用
    CSS\u选择器
    I:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#WIN_3_1002 > div.btntextdiv > div.f1"))).click()
    
  • 使用CSS_选择器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,因为href触发的某些javascript代码位于a标记而不是div上

      driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002').click()
或者试试动作课

    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)

尝试
element.submit()
感谢您的回复,我不知道该方法,但是将click()替换为submit(),我得到了“Message:not location element:./sensor or self::form”感谢您的回复,但是我已经尝试了隐式和显式等待,但是警告不断出现。。。我相信问题可能与javascript或警告框中写的东西有关。@Raphael检查更新的答案并告诉我状态。警告也出现了;((感谢您的回复,我不知道tag和div之间的区别……但是由于您的建议,我无法解决此问题。我相信此问题可能与javascript或警告框中写的内容有关。@Raphael您是否有指向该网站的链接,我们可以试用它?很抱歉,不,您将无法使用此链接。)要访问该网站,因为它是通过vpn访问的,您需要凭据。对于action chain和click,您都会收到javascript错误?是的,正如我在问题中所说,这很奇怪,因为在其他页面上,单击按钮的代码可以工作,如果我在单击()之前单击,它也可以工作((