0

In this below page excerpt, I need to be able to click 'App Profiles'. But what ever I try I cannot get it to work.

I keep getting OpenQA.Selenium.NoSuchElementException.

<body>
  <div id="main-page">
    <div class="page"></div>
    <div id="page-header" class="page-header">
      <div id="page-header-navbar" class="page-header-navbar">
        <div class="page-header-contents">
          <div class="page-header-navbar-left">
            <div class="nav-menus">
              <div class="nav-menu" data-item="dashboard">
                <div class="nav-menu-header selected -js-header">
                  <i class="nav-menu-icon fa fa-tachometer"></i>
                  <span class="nav-menu-header-label">Dashboard</span>
                </div>
              </div>
              <div class="nav-menu" data-item="policy">
                <div class="nav-menu-header -js-header">
                  <i class="nav-menu-icon fa fa-sitemap"></i>
                  <span class="nav-menu-header-label">App Profiles</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

These have been tried:

IWebElement element = driver.FindElement(By.XPath("//div\[@class='nav-menu-header-label' and text()='App Profiles'\]"));        
IWebElement appProfilesElement = driver.FindElement(By.XPath("//span\[text()='App Profiles'\]"));     
1
  • Did you try to wait for element to be fully loaded in page? Commented Jun 3, 2024 at 15:40

3 Answers 3

0

Try wait first:

from selenium.webdriver.support.ui import WebDriverWait
# wait for 10 seconds
wait = WebDriverWait(driver, 10)
xpath = "//div[@class='nav-menu-header-label' and text()='App Profiles']"
element = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
element.click()
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Arthur, thank you for your quick reply. But unfortunately: An unhandled exception of type 'OpenQA.Selenium.WebDriverTimeoutException' occurred in WebDriver.dll: 'Timed out after 10 seconds' at OpenQA.Selenium.Support.UI.DefaultWait1.ThrowTimeoutException(String exceptionMessage, Exception lastException) at OpenQA.Selenium.Support.UI.DefaultWait1.Until[TResult](Func2 condition, CancellationToken token) at OpenQA.Selenium.Support.UI.DefaultWait1.Until[TResult](Func`2 condition)
I would debug in two aspects: 1. make sure this item can be clickable; 2. try longer wait time - extend the 10 seconds to 30 seconds
Hey Arthur, as troubleshooting i have started enumerating all elements on the page. And now i see that there is something wrong. So prior to this code i already clicked a link that opens a new TAB. And what i now see is that the code is reporting elements from the first page...... So how do i 'move along' with the new tab?
0

I'm not sure how you got the XPath, but it does not look correct. Using the same code, I've got the XPath: //*[@id="page-header-navbar"]/div/div/div/div[2]/div/span

You can simply Open F12 Developer Tools --> Inspect the element --> Locate the element --> Right click on it --> Copy --> Copy XPath.

So it should be: driver.FindElement(By.XPath("//*[@id=\"page-header-navbar\"]/div/div/div/div[2]/div/span"));

Now I can get the element without any error message.

1 Comment

Hi Kendrick, Thank you for the reply. My issue was that I just not had the Driver 'focus' on the correct TAB. I then followed the instructions on Selenium to fix this. But with regards to selecting the element your answer is absolutely correct! I used the same Inspection but then used --> Copy Element to come up what i'm using, and this is also working. I guess it is a matter of preference. string xpathExpressionAP = "//span[@class='nav-menu-header-label' and text()='App Profiles']"; IWebElement elementAP2 = driver.FindElement(By.XPath(xpathExpressionAP)); elementAP2.Click();
0

What is not in the code above is a click that opened a new TAB in the browser. So the issue was that the Selenium driver did not had the reference/focus to the correct TAB. Once I followed the Selenium guidance I could get the reference to the correct element. https://www.selenium.dev/documentation/webdriver/interactions/windows/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.