5

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> 
13
  • What error are you getting?
    – Suzanne
    CommentedJan 13, 2017 at 5:46
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"btnNew"}
    – user2731629
    CommentedJan 13, 2017 at 5:53
  • 1
    @user2731629 are there any iframe elements on a page? Check if this button is not inside an iframe.
    – alecxe
    CommentedJan 13, 2017 at 5:56
  • @user2731629 also, for debugging purposes only, does putting a time.sleep(5) before locating the button make any difference?
    – alecxe
    CommentedJan 13, 2017 at 5:56
  • can you share some 4-5 lines of html of page above the current line
    – thebadguy
    CommentedJan 13, 2017 at 6:58

3 Answers 3

6

I am guessing this is a frames problem.

Even if you have a perfectly-correct XPATH, Selenium won't see the element unless you're already in the frame that contains the element.

To quickly see how many frames are on the page you're on:

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

If you then find out there are, say, three frames, you could do:

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

or

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

or

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

    You can use the following ways to locate your element

    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 

    Please refer to the documentation at http://selenium-python.readthedocs.io/locating-elements.html

    Some examples of the same are as under

    1. By CSS Selector

    driver.find_element_by_css_selector('.button.c_button.s_button').click() 

    2. By Name

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

    3. By ClassName

    driver.find_element_by_class_name('s_image').click() 
    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"}
      – user2731629
      CommentedJan 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.
      – AurA
      CommentedJan 13, 2017 at 5:52
    0

    You should try to add some waiting time before clicking button as follow:

    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(); 

    With above code you'll be able to wait up to 20 seconds for target button to become clickable. Otherwise, you'll get TimeOutException

    If this solution won't work, check whether your button located inside <iframe> element. If so, switch to that frame before clicking button:

    driver.switch_to_frame('iframe_id') 
    1
    • I will try.. Thanks,,
      – user2731629
      CommentedJan 13, 2017 at 8:40