硒click()-选择按钮但不单击 [英] Selenium click() - selects the button but doesn't click
问题描述
我正在使用Selenium Python来执行类似Robotic Process Automation的操作.但是,我在点击按钮时遇到问题...当我手动单击 Search 按钮时,什么也没有发生,但是通过Selenium出现了警报:
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:
我正在使用的代码是:
try:
driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)').click()
except Exception as e:
print(e)
按钮的html部分为:
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>
这很奇怪,因为对于相同的按钮,我在其他页面上也有类似的代码.
It's strange because I have a similar code that works on other pages, for the same button.
类似按钮的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>
如果您对我的代码的质量以及如何解决此问题有任何建议,我将不胜感激.
If you have advices on the quality of my code and how to fix this problem, I would be grateful.
推荐答案
单击a标记而不是div,因为href触发某些JavaScript代码位于标记上,而不是div.
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()
或者尝试使用动作类
elem = driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)')
Webdriver.ActionChain(driver).move_to_element(elem).click()
尝试使用javascript执行程序:
try javascript executor:
driver.execute_script("arguments[0].click()",elem)
这篇关于硒click()-选择按钮但不单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!