Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

click javascript button using selenium Python

I am new to Selenium Python and JavaScript. I am trying to automate the webpage navigations using Selenium python. I want to click one Java script button. Below is the html tag.

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

How to click the 'New..' button from Selenium Python. I already tried below things, but getting error.

driver.find_element_by_id('btnNew')

and

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

snippet of HTML as requested

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

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
2
  • I tried Everything. But nothing worked. Getting below error. selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"btnNew"} Commented Jan 13, 2017 at 5:50
  • Use this when you know id attribute of an element. With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised. Commented Jan 13, 2017 at 5:52

default