Search code examples
pythonseleniumxpathwebautomation

How to find element via XPATH (already found it using console)


I'm working on navigating a webpage in Edge (but Chrome would work too), and need to click an element, which drops a menu, and then select an item on that menu.

Python and Selenium are having issues locating the button on the site. Here is an HTML screenshot and the console entry using the XPATH to find the element.

Formatting my HTML is giving me some issues too, but here it is if it's helpful:

<div id="ext-comp-1051-targetEl" class="x-box-target" role="presentation" style="width: 995px;"><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1054" style="margin: 0px; right: auto; left: 0px; top: 0px;"><span id="button-1054-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1054-btnEl" class="x-btn-button" role="presentation"><span id="button-1054-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">Research</span><span role="presentation" id="button-1054-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1064" style="margin: 0px; right: auto; left: 104px; top: 0px;"><span id="button-1064-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1064-btnEl" class="x-btn-button" role="presentation"><span id="button-1064-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">FOT/ACATS</span><span role="presentation" id="button-1064-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1070" style="margin: 0px; right: auto; left: 226px; top: 0px;"><span id="button-1070-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1070-btnEl" class="x-btn-button" role="presentation"><span id="button-1070-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">Financials</span><span role="presentation" id="button-1070-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1080" style="margin: 0px; right: auto; left: 334px; top: 0px;"><span id="button-1080-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1080-btnEl" class="x-btn-button" role="presentation"><span id="button-1080-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">Reconciliation</span><span role="presentation" id="button-1080-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1096" style="margin: 0px; right: auto; left: 466px; top: 0px;"><span id="button-1096-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1096-btnEl" class="x-btn-button" role="presentation"><span id="button-1096-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">Maintenance</span><span role="presentation" id="button-1096-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1103" style="margin: 0px; right: auto; left: 591px; top: 0px;"><span id="button-1103-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1103-btnEl" class="x-btn-button" role="presentation"><span id="button-1103-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">Productivity Tools</span><span role="presentation" id="button-1103-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><a class="x-btn alloy-nav-item x-unselectable x-btn-toolbar x-box-item x-toolbar-item x-btn-plain-toolbar-small x-noicon x-btn-noicon x-btn-plain-toolbar-small-noicon" hidefocus="on" unselectable="on" tabindex="0" id="button-1106" style="margin: 0px; right: auto; left: 744px; top: 0px;"><span id="button-1106-btnWrap" role="presentation" class="x-btn-wrap x-btn-arrow x-btn-arrow-right" unselectable="on"><span id="button-1106-btnEl" class="x-btn-button" role="presentation"><span id="button-1106-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on">Admin</span><span role="presentation" id="button-1106-btnIconEl" class="x-btn-icon-el  " unselectable="on" style=""></span></span></span></a><div class="x-component x-box-item x-toolbar-item x-component-default" id="tbfill-1053" style="margin: 0px; right: auto; left: 828px; width: 125px; top: 13px;"></div><div class="x-component alloy-icon settings-btn x-box-item x-toolbar-item x-component-default" id="component-1116" style="right: auto; left: 961px; margin: 0px; top: 0px;"><i class=" alloy-icon-cog alloy-icon-lg alloy-icon-over"></i></div></div>

I've tried looking by class, id, XPATH etc. and simply cannot find it. I've also tried using the Select and By mods of Selenium. Confirmed there are no iframes on the site and that the element exists (waited for) and I've looked through A LOT of similar questions on SO and other sites.

Here's the code I'm using (two separate attempts, obviously):

driver.find_element_by_xpath("//span/div/div/div/div/a[4]")
driver.find_element_by_xpath("//*[@id='button-1080']")

and the error I get:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div/div/span/div/div/div/div/a[4]"}
  (Session info: MicrosoftEdge=91.0.864.41)
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='button-1080']"}
  (Session info: MicrosoftEdge=91.0.864.41)

I have some issues sharing too much more since the site and my code is work related, but I'm quite sure that the code up to this point is correct.


Solution

  • Sounds like you need to use Explicit wait :

    If Reconciliation you want to click on, I would suggest to use LINK_TEXT or PARTIAL_LINK_TEXT :

    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT , 'Reconciliation')))
    element.click()
    

    Imports :

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

    PS :

    go to developer tool first (Press F12) and then navigate to Elements sections and do CTRL+F and then in search bar search for this xpath //span[text()='Reconciliation'] if there's only 1 entry then you should be able proceed further with the above code.