Search code examples
pythonselenium-webdriverdynamic

Select dynamic element using Python


I am trying to click on an element present in a dynamic table (that changes its size for each page). (Sorry if this is a very basic question, I'm new using Selenium)

<html><head>
    <title>frmMeterItem</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <link href="css/main.css?20250226" type="text/css" rel="stylesheet">
    <script language="javascript" src="js/Util.js?20250226" type="text/javascript"></script>
</head>
<body class="bodyInfo">
    <form name="Form1" method="post" action="./frmMeterItemMenu.aspx?MeterId=1913" id="Form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEzMDg2OTU4MzAPZBYCZg9kFgICAQ9kFgJmDw8WBh4LQ2VsbFBhZGRpbmdmHgtDZWxsU3BhY2luZ2YeBF8hU0ICgIAYZBYCZg9kFiRmDw8WBh4FV2lkdGgbAAAAAAAA8D8BAAAAHgZIZWlnaHQbAAAAAAAAN0ABAAAAHwICgANkZAIBDw8WBB4PSG9yaXpvbnRhbEFsaWduCyopU3lzdGVtLldlYi5VSS5XZWJDb250cm9scy5Ib3Jpem9udGFsQWxpZ24CHwICgIAEFgIeB29uQ2xpY2sFL2xvY2F0aW9uLmhyZWY9J2ZybU1ldGVySXRlbU1lbnUuYXNweD9UYWI9TWV0ZXInZAICDw8WBh8DGwAAAAAAAPA/AQAAAB8EGwAAAAAAADdAAQAAAB8CAoADZGQCAw8PFgQfBQsrBAIfAgKAgAQWCB4Lb25tb3VzZWRvd24FGW1fZG93bl9idG4odGhpcywnbWVudTInKTseC29ubW91c2VvdmVyBRltX292ZXJfYnRuKHRoaXMsJ21lbnUyJyk7Hgpvbm1vdXNlb3V0BRhtX291dF9idG4odGhpcywnbWVudTInKTsfBgU1bG9jYXRpb24uaHJlZj0nZnJtTWV0ZXJJdGVtTWVudS5hc3B4P1RhYj1M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5336B38">
    <div class="divMenu2"><table id="_ctl1" class="tableMenu23" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
    <tbody><tr>
        <td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl2" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2_sel" nowrap="nowrap" align="center" onclick="location.href='frmMeterItemMenu.aspx?Tab=Meter'"><span id="_ctl3" class="labelLink">Characteristics</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl4" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2" nowrap="nowrap" align="center" onmousedown="m_down_btn(this,'menu2');" onmouseover="m_over_btn(this,'menu2');" onmouseout="m_out_btn(this,'menu2');" onclick="location.href='frmMeterItemMenu.aspx?Tab=LoadProfile'"><span id="_ctl5" class="labelLink ai-style-change-1">Load profile</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl6" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2_disabled" nowrap="nowrap" align="center"><span id="_ctl7">Logbook</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl8" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2" nowrap="nowrap" align="center" onmousedown="m_down_btn(this,'menu2');" onmouseover="m_over_btn(this,'menu2');" onmouseout="m_out_btn(this,'menu2');" onclick="location.href='frmMeterItemMenu.aspx?Tab=EOB'"><span id="_ctl9" class="labelLink">End of billing</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl10" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2_disabled" nowrap="nowrap" align="center"><span id="_ctl11">Instant. data</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl12" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2" nowrap="nowrap" align="center" onmousedown="m_down_btn(this,'menu2');" onmouseover="m_over_btn(this,'menu2');" onmouseout="m_out_btn(this,'menu2');" onclick="location.href='frmMeterItemMenu.aspx?Tab=InfoTerrain'"><span id="_ctl13" class="labelLink">Diagnostic</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl14" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2_disabled" nowrap="nowrap" align="center"><span id="_ctl15">Power Quality</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl16" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2_disabled" nowrap="nowrap" align="center"><span id="_ctl17">Tampering</span></td><td nowrap="nowrap" style="height:23px;width:1px;"><img id="_ctl18" src="img/fond_bouton2_fil.jpg" border="0" style="height:23px;width:1px;"></td><td class="menu2_disabled" nowrap="nowrap" align="center"><span id="_ctl19">Waveform</span></td>
    </tr>
</tbody></table></div>
<script language="javascript">
parent.frameMain.location.href='frmMeterItem.aspx?FirstAccess=&FormMode=0'
</script>
<script language="javascript">

        function resizePage()
        {
          try
          {
            var iHeight;
            var oElement = document.getElementById('_ctl0');
            if (oElement && (oElement.className == 'divMainPage'))
              iHeight = -14;
            else if (oElement && (oElement.className == 'divMainPage2 divMainPage'))
              iHeight = -39;
                        else if (oElement && (oElement.className == 'divMainPage3 divMainPage'))
              iHeight = -39;
            else
            {
              
              oElement = document.getElementById('_ctlDiv');
              if (oElement != null)
              {
                if (oElement.className == 'divMainPage2 divMainPage')
                  iHeight = m_bGecko ? -29 : -39;  // Spécifique page widget
                else
                  iHeight = m_bGecko ? -2 : -12;  // Spécifique page widget
              }
              else
              {
                if (oElement == null)
                  oElement = document.getElementById('ctlDivMain');
                if (oElement == null)
                  oElement = document.getElementById('_ctl1');
                if (oElement && (oElement.className == 'divMainPage'))
                  iHeight = -14;
                else if (oElement && (oElement.className == 'divMainPage2 divMainPage'))
                  iHeight = -39;
                                else if (oElement && (oElement.className == 'divMainPage3 divMainPage'))
                  iHeight = -39;
              }
            }

            if (oElement)
            {
              if (m_bIE && (getInternetExplorerVersion() >= 10))
                iHeight -= 17;
              iHeight = iHeight + frameElement.offsetHeight;
              if ((navigator.userAgent.indexOf('MSIE') != -1) && (parseInt(navigator.userAgent.split('MSIE')[1]) >= 10))  // If IE10
                iHeight = iHeight - 17;
              if (m_bGecko)
                iHeight = (iHeight - 14) + 'px';
              oElement.style.height = iHeight;
            }
          }
          catch (x) { }
        }
        if (window.name == 'frameMain')
        {
          if (window.onload) {
            var oldOnload = window.onload;
            window.onload = function() { resizePage(); oldOnload(); }
          } else window.onload = resizePage;

          if (window.onresize) {
            var oldResize = window.onresize;
            window.onresize = function() { resizePage(); oldResize(); }
          } else window.onresize = resizePage;
        }
        
</script>
</form>
</body></html>

This is the menu rendered: enter image description here

I have tried different approaches that supposedly work for dynamic web elements but with no success. I'm stuck and would deeply appreciate if someone could help me!

  1. Through the CSS selector enter image description here

     selector = "#_ctl1 > tbody > tr > td:nth-child(4)"
     element = WebDriverWait(driver, 10).until(
         EC.element_to_be_clickable((By.CSS_SELECTOR, selector))
     )
     element.click()
    

Error message:

Stacktrace:
        GetHandleVerifier [0x003D0B43+25139]
        (No symbol) [0x003613F4]
        (No symbol) [0x002404E3]
        (No symbol) [0x002883D7]
        (No symbol) [0x0028872B]
        (No symbol) [0x002D1002]
        (No symbol) [0x002AD014]
        (No symbol) [0x002CE778]
        (No symbol) [0x002ACDC6]
        (No symbol) [0x0027BDE9]
        (No symbol) [0x0027D124]
        GetHandleVerifier [0x006D4373+3185251]
        GetHandleVerifier [0x006F291A+3309578]
        GetHandleVerifier [0x006ECF42+3286578]
        GetHandleVerifier [0x00467AE0+643536]
        (No symbol) [0x0036A20D]
        (No symbol) [0x003670B8]
        (No symbol) [0x00367257]
        (No symbol) [0x00359E00]
        BaseThreadInitThunk [0x759D5D49+25]
        RtlInitializeExceptionChain [0x77BCCDEB+107]
        RtlGetAppContainerNamedObjectPath [0x77BCCD71+561]
  1. Through the full XPATH: enter image description here

    wait = WebDriverWait(driver, 10)
    
    element = wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/form/div/table/tbody/tr/td[4]")))
    element.click()
    

Error message: same as in 1

  1. Through the text that it contains:

    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Load profile')]")))
    element.click()
    

Error message:

no such element: Unable to locate element: {"method":"xpath","selector":"//td[contains(text(),'Load profile')]"}
  (Session info: chrome=133.0.6943.128); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
        GetHandleVerifier [0x003D0B43+25139]
        (No symbol) [0x003613F4]
        (No symbol) [0x002404E3]
        (No symbol) [0x002883D7]
        (No symbol) [0x0028872B]
        (No symbol) [0x002D1002]
        (No symbol) [0x002AD014]
        (No symbol) [0x002CE778]
        (No symbol) [0x002ACDC6]
        (No symbol) [0x0027BDE9]
        (No symbol) [0x0027D124]
        GetHandleVerifier [0x006D4373+3185251]
        GetHandleVerifier [0x006F291A+3309578]
        GetHandleVerifier [0x006ECF42+3286578]
        GetHandleVerifier [0x00467AE0+643536]
        (No symbol) [0x0036A20D]
        (No symbol) [0x003670B8]
        (No symbol) [0x00367257]
        (No symbol) [0x00359E00]
        BaseThreadInitThunk [0x759D5D49+25]
        RtlInitializeExceptionChain [0x77BCCDEB+107]
        RtlGetAppContainerNamedObjectPath [0x77BCCD71+561]

I have also tried to switch to the frame "frameInfo" before finding it by text but getting the same error message as in 1.

For all, I get the warning:

Created TensorFlow Lite XNNPACK delegate for CPU.
Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors (tensor#-1 is a dynamic-sized tensor).

Edit: I have added the source html and more ways that I have tried. I cannot share the html as this is a local page.


Solution

  • You mention a form inside a frame. You likely need to select the iframe first. I would use this guide to learn about how to select the frame using switchTo.

    Then try your code

    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Load profile')]")))
    element.click()
    

    The html your provided does not seem to match the problem you are trying to solve.