gpt4 book ai didi

javascript - 使用 selenium Python 单击 javascript 按钮

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:11 27 4
gpt4 key购买 nike

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

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

如何从 Selenium Python 中单击“新建...”按钮。我已经尝试过以下操作,但出现错误。

driver.find_element_by_id('btnNew')

NEXT_BUTTON_XPATH = '//input[@type="button" and @id="btnNew"]'
button = driver.find_element_by_xpath(NEXT_BUTTON_XPATH)
button.click()

请求的 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>

最佳答案

我猜这是一个框架问题。

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

要快速查看您所在页面上有多少帧:

a = driver.find_elements_by_css_selector("frame")
print(len(a))

如果您随后发现有,比如说,三帧,您可以:

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

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

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

关于javascript - 使用 selenium Python 单击 javascript 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com