'Selenium cant find an element unless the page is inspected

I am automating with selenium and noticed something weird, on a specific webpage, there is a ul list containing some li roles that are buttons. For some reason, selenium cannot find any of the li's or even the ul unless I inspect element anywhere on the page. I can literally click any other button or interact anywhere, but if I never interact manually with the ul (by manually clicked the li buttons, or inspecting element) selenium cannot find it. please help.

Here is the html code

<ul class="GE0S-T1CMSI mm_tabStrip cw-tab-bar">
  <li class="GE0S-T1CCSI GE0S-T1CNSI cw-tab-active">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">Ticket</span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Tasks&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
      </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Configurations&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">1</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Products&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Activities&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Time&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Expenses&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Schedule&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">2</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Attachments&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Open Tickets&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">34</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Conversions&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">Audit Trail</span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      Surveys&nbsp; &nbsp;
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      RMA&nbsp; &nbsp; 
      <span class="GE0S-T1CPRI">0</span>
    </span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">Unite</span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">Automate Network Map</span>
  </li>
  <li class="GE0S-T1CCSI">
    <a class="GE0S-T1CPSI"></a>
    <span class="GE0S-T1CCTI">
      <div onload="this.__gwtLastUnhandledEvent=&quot;load&quot;;" class="cwsvg mm_icon" style="">
        <svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 48 48" 
        focusable="false" viewBox="0 0 48 48">
          <g>
            <path d="M24 17c-3.9 0-7 3.1-7 7s3.1 7 7 7 7-3.1 7-7-3.1-7-7-7zm0 10c-1.7 0-3-1.3-3-                        
            3s1.3-3 3-3 3 1.3 3 3-1.3 3-3 3zm17-.2l-2.7-2.1c0-.2 0-.5 0-.8s0-.5 0-.8L41 21c1-.8 
            1.3-2.2.7-3.4L38.4 12c-.6-1.1-2-1.6-3.2-1.2l-3.3 1.3c-.5-.3-.9-.6-1.4-.8L30 8c-.2-1- 
            1.3-2-2.7-2h-6.7c-1.3 0-2.4 1-2.6 2.2l-.5 3.3c-.5.2-.9.5-1.4.8L12.8 11c-1.2-.5-2.6 0- 
            3.2 1.1l-3.3 5.6c-.6 1.2-.3 2.7.7 3.5l2.7 2.1c0 .3 0 .5 0 .8s0 .5 0 .8L7 26.8c-1 .8- 
            1.3 2.2-.7 3.4l3.3 5.6c.6 1.1 2 1.6 3.2 1.2l3.3-1.3c.5.3.9.6 1.4.8l.5 3.3c.2 1.3 1.3 
            2.3 2.6 2.3h6.7c1.3 0 2.4-1 2.6-2.2l.5-3.3c.5-.2.9-.5 1.4-.8l3.3 1.3c1.2.5 2.6 0 3.2- 
            1.1l3.4-5.6c.6-1.4.3-2.8-.7-3.6zm-6.4-4.2c.1.6.1 1 .1 1.4s0 .8-.1 1.4l-.1 1 3.6 2.8- 
            2.5 4.1-4.3-1.7-.8.6c-.8.6-1.6 1-2.4 1.4l-1 .4-.7 4.5h-5l-.7-4.5-1-.4c-.8-.3-1.6-.8- 
            2.4-1.4l-.8-.6-4.3 1.7-2.5-4.1 3.6-2.8-.1-1c-.1-.5-.1-1-.1-1.4s0-.8.1-1.4l.1-1-3.6-2.8 
            2.5-4.1 4.3 1.7.8-.6c.8-.6 1.6-1 2.4-1.4l1-.4.7-4.5h5l.7 4.5 1 .4c.8.3 1.6.8 2.4 
            1.4l.8.6 4.3-1.7 2.5 4.1-3.6 2.8.1 1z"></path>
            <path d="M0 0h48v48H0z" fill="none"></path>
          </g>
        </svg>
      </div>
    </span>
  </li>
  <li class="GE0S-T1CASI"></li>
  <div class="x-clear"></div>
  </ul>

Also idk how to copy and paste html code with the indents so yes I spent 30 minutes manually indenting all of that.

but please help

the code I am using is

WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[2]/div[2]/div/div[1]/div/div[3]/div/div[3]/div/div[2]/div/div[2]/div/div[2]/div/div[1]/div[3]/ul/li[6]/span')))
browser.find_element(by=By.XPATH,value='/html/body/div[2]/div[2]/div/div[1]/div/div[3]/div/div[3]/div/div[2]/div/div[2]/div/div[2]/div/div[1]/div[3]/ul/li[6]/span').click()

It works perfectly but only after I inspect the webpage, or if I click the button once manually

the website is connectwise which is my companies ticketing system so I can't provide an exact link.



Solution 1:[1]

  1. You should use a better way to find the element on the page instead of using absolute XPATH (more information here)

XPATH expression:

//*[contains(text(),'text here')]

In your case:

//span[contains(text(),'Products')]
  1. Check on your page, to ensure the button can be clicked before or after doing something (Inspect HTML and see what happened when you do that). If yes, perform that action before/after clicking on the button. I recommend that because this html cw-tab-bar and cw-tab-actice
    <ul class="GE0S-T1CMSI mm_tabStrip cw-tab-bar">
    <li class="GE0S-T1CCSI GE0S-T1CNSI cw-tab-active">
  1. I suggest using another wait condition (read here for more information)

element_to_be_clickable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan