使用selenium Python单击javascript按钮

使用selenium Python单击javascript按钮,javascript,python,selenium,Javascript,Python,Selenium,我不熟悉Selenium Python和JavaScript。我正在尝试使用Selenium python自动化网页导航。我想单击一个Java脚本按钮。下面是html标记 <button type="button" class="hpButton" onclick="loadAddPage()" id="btnNew" tabindex="100" style="width: 83px;">New...</button> 及 按要求提供HTML代码片段 <scri

我不熟悉Selenium Python和JavaScript。我正在尝试使用Selenium python自动化网页导航。我想单击一个Java脚本按钮。下面是html标记

<button type="button" class="hpButton" onclick="loadAddPage()" id="btnNew" tabindex="100" style="width: 83px;">New...</button>

按要求提供HTML代码片段

<script type='text/javascript' language='JavaScript'>
var FORM_URL_ID_MY_DISCOVERY_LIST = '/mxportal/settings/AutoDiscoveryList.jsp';</script>
<script type='text/javascript' language='JavaScript'>
window.SELECTABLE_ID_MY_DISCOVERY_LIST = true;window.PRESELECTED_ID_MY_DISCOVERY_LIST = true;window.PRESELECTION_DEFAULT_ID_MY_DISCOVERY_LIST = true;</script>
<div id='ID_MY_DISCOVERY_LIST_headerDiv' class='headerDivOuter'><div class='headerDivInner'><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=91787367&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'></div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=0&sortUp=false")' class='visualColumnHeader sortedAscending'><div class='pad0x5'>Name</div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=1&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'>Last Run</div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=2&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'>Schedule</div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=3&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'>Comments</div></div></div></div><div id='ID_MY_DISCOVERY_LIST_scrollingTableDiv' style='clear:both;overflow:visible'><table id='ID_MY_DISCOVERY_LIST' cellspacing='0' class='scrollingTable' cellpadding='0'><thead><tr class='readerCaptionRow'><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=91787367&sortUp=true")' class='checkboxCell sortable'></th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=0&sortUp=false")' class='sortedAscending' scope='col'>Name</th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=1&sortUp=true")' class='sortable' scope='col'>Last Run</th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=2&sortUp=true")' class='sortable' scope='col'>Schedule</th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=3&sortUp=true")' class='sortable' scope='col'>Comments</th></tr></thead><tbody><tr id='ID_MY_DISCOVERY_LIST_dummyRow' class='dummyRow'><td><div></div></td><td class='sorted'><div></div></td><td><div></div></td><td><div></div></td><td><div></div></td></tr><tr class='altRowColor rowHighlight'><td scope='row' class='checkboxCell'><input rowselector='yes' checked name='ID_MY_DISCOVERY_LIST_selector' type='RADIO'></td><td class='sorted'>ESX VC</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>Hyper-vHost</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr class='altRowColor'><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>OA</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>OA 42.35</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr class='altRowColor'><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>System Automatic Discovery</td><td>Never</td><td><b>Task is Disabled</b> - Periodic</td><td></td></tr><tr style='display:none' class='noDataRow'><td colspan='5'>No discovery tasks defined.</td></tr></tbody></table></div>
        <div style="margin-top:15px">
            <div class='buttonSet'>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton'  onclick='runNowStop()' id='btnRunNowStop' tabindex='105'>Run Now</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton'  onclick='viewTaskResults()' id='btnViewTaskResults' tabindex='104'>View Task Results...</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton'  onclick='confirmDeleteDiscoveryTask()' id='btnDelete' tabindex='103'>Delete...</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton'  onclick='enableDisableDiscoveryTask()' id='btnEnableDisable' tabindex='102'>Disable</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton'  onclick='loadEditPage()' id='btnEdit' tabindex='101'>Edit...</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton'  onclick='loadAddPage()' id='btnNew' tabindex='100'>New...</button></div></div></div>
</div>
<div class='clearFloats'></div>

var FORM_URL_ID_MY_DISCOVERY_LIST='/mxportal/settings/AutoDiscoveryList.jsp';
window.selective\u ID\u MY\u DISCOVERY\u LIST=true;window.PRESELECTED\u ID\u MY\u DISCOVERY\u LIST=true;window.PRESELECTION\u DEFAULT\u ID\u MY\u DISCOVERY\u LIST=true;
NameLast RunScheduleCommentsX VC1/13/17 11:00 AMPeriodic-下一次运行:1/14/17 11:00 AMHyper-vHost1/13/17 11:00 AMPeriodic-下一次运行:1/14/17 11:00 AMOA1/13/17 11:00 AMPeriodic-下一次运行:1/14/17 11:00 AMOA 42.351/13/17 11:00 AMPeriodic-下一次运行:1/14/17 11:00 AMSystem Automatic DiscoveryNeverTask已禁用-Periodic未定义发现任务。
快跑
查看任务结果。。。
删除。。。
使残废
编辑
新的。。。

您可以使用以下方法定位元素

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
请参阅

下面是一些相同的例子

1。按CSS选择器

driver.find_element_by_css_selector('.button.c_button.s_button').click()
2。按姓名排列

driver.find_element_by_name('s_image').click()
driver.find_element_by_class_name('s_image').click()
3。按类名

driver.find_element_by_name('s_image').click()
driver.find_element_by_class_name('s_image').click()

您应该尝试在单击按钮之前添加一些等待时间,如下所示:

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

button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "btnNew")));
button.click();
使用上述代码,您可以等待20秒,目标按钮才可单击。否则,您将得到
TimeOutException

如果此解决方案不起作用,请检查按钮是否位于
元素内。如果是,请在单击按钮之前切换到该帧:

driver.switch_to_frame('iframe_id')

我猜这是一个框架问题

即使您拥有完全正确的XPATH,Selenium也不会看到该元素,除非您已经在包含该元素的框架中

要快速查看页面上有多少帧,请执行以下操作:

a = driver.find_elements_by_css_selector("frame")
print(len(a))
如果你发现有,比如说,三帧,你可以做:

driver.switch_to_frame(a[0])
button.click()


您遇到了什么错误?
selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法定位元素:{“方法”:“id”,“选择器”:“btnNew”}
@user2731629页面上有iframe元素吗?检查此按钮是否不在iframe中。@user2731629另外,仅出于调试目的,在定位按钮之前放置
时间。睡眠(5)
是否有任何区别?能否在当前行上方共享页面的4-5行html?我尝试了所有方法。但什么都没用。下面是一个错误
selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法定位元素:{“方法”:“id”,“选择器”:“btnNew”}
当您知道元素的id属性时使用此选项。使用此策略,将返回id属性值与位置匹配的第一个元素。如果没有元素具有匹配的id属性,则将引发NoTouchElementException。我将尝试。。谢谢
driver.switch_to_frame(a[1])
button.click()
driver.switch_to_frame(a[2])
button.click()