title: each variables match I need to click a particular link and get data
body: <p>I have list which has multiple links. Each variables match I need to click a particular link and get data.</p>
  
  <p>I have written the below code. When it executes, it gives me <strong>only AAA data (and not BBB</strong>) and a <strong><code>stale element reference: element is not attached to the page document</code></strong> error.</p>
  
  <p>The reason for this is because the element to which you have referred is removed from the DOM structure but I have no idea how.</p>
  
  <pre class="lang-py prettyprint-override"><code>    soup = BeautifulSoup(driver.page_source, "lxml")
  
      mypage_url  = driver.current_url
      myitems = soup.select('#myitemList a')
      for myitem in myitems:
          print(myitem.text)
          item_url = urljoin(mypage_url, myitem.attrs["href"])
          driver.get(item_url)
          sleep(1)
          driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  
          A = "AAA"
          B = "BBB"
  
          trs = driver.find_elements_by_css_selector("#item_contents &gt; table &gt; tbody &gt; tr")
          for i in range(1,len(trs)):
              names = trs[i].find_element_by_css_selector("td &gt; p &gt; span")
  
              sleep(1)
  
              if A  in names.text:
                  trs[i].find_element_by_xpath("td/ul/li[1]/input").click()
                  sleep(2)
                  print(A)
              elif B in names.text:
                  trs[i].find_element_by_xpath("td/ul/li[1]/input").click()
                  sleep(2)
                  print(B)
                  break
      driver.close()
      driver.quit()
  </code></pre>
  
  <p>This is the HTML structure is as below</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div id="myitem" class="panel"&gt;
    &lt;div class="side-box"&gt;
      &lt;p class="tt"&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
      &lt;div class="ct"&gt;
        &lt;ul id="myitemList"&gt;
          &lt;li id="myitem_21" class="itname"&gt;
            &lt;div style=""&gt;
              &lt;a href=""&gt;shop1&lt;/a&gt;
            &lt;/div&gt;
            &lt;div style=""&gt;&lt;/div&gt;
          &lt;/li&gt;
          &lt;li id="myitem_23" class="itname"&gt;
            &lt;div style=""&gt;
            &lt;a href=""&gt;shop2&lt;/a&gt;
            &lt;/div&gt;
            &lt;div style=""&gt;&lt;/div&gt;
          &lt;/li&gt;
          &lt;li id="myitem_54" class="itname"&gt;
            &lt;div style=""&gt;
              &lt;a href=""&gt;shop3&lt;/a&gt;
            &lt;/div&gt;
            &lt;div style=""&gt;&lt;/div&gt;
          &lt;/li&gt;
          &lt;li id="myitem_94" class="itname"&gt;
            &lt;div style=""&gt;
              &lt;a href=""&gt;shop4&lt;/a&gt;
            &lt;/div&gt;
            &lt;div style=""&gt;&lt;/div&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
      &lt;span class="bt"&gt;&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;tbody&gt;
    &lt;tr&gt;
      &lt;th id="title"&gt;&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="clear"&gt;
       &lt;p style=""&gt;
         &lt;span&gt;AAA
           &lt;/span&gt;
       &lt;/p&gt;
       &lt;ul class="clear"&gt;
         &lt;li&gt;&lt;input type="button" name="select" value="" onclick="listClick();"&gt;&lt;/li&gt;
           &lt;li&gt;&lt;input type="button" name="list" value="" onclick="modelClick()"&gt;&lt;/li&gt;
       &lt;/ul&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="altn"&gt;
      &lt;td class="clear"&gt;
        &lt;p style=""&gt;
          &lt;span&gt;BBB
           &lt;/span&gt;
        &lt;/p&gt;
        &lt;ul class="clear"&gt;
         &lt;li&gt;&lt;input type="button" name="select" value="" onclick="listClick();"&gt;&lt;/li&gt;
           &lt;li&gt;&lt;input type="button" name="list" value="" onclick="modelClick()"&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="clear"&gt;
        &lt;p style=""&gt;
          &lt;span&gt;CCC
            &lt;/span&gt;
        &lt;/p&gt;
        &lt;ul class="clear"&gt;
         &lt;li&gt;&lt;input type="button" name="select" value="" onclick="listClick();"&gt;&lt;/li&gt;
           &lt;li&gt;&lt;input type="button" name="list" value="" onclick="modelClick()"&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="altn"&gt;
        &lt;td class="clear"&gt;
          &lt;p style=""&gt;
           &lt;span&gt;CCC
             &lt;/span&gt;
          &lt;/p&gt;
          &lt;ul class="clear"&gt;
           &lt;li&gt;&lt;input type="button" name="select" value="" onclick="listClick();"&gt;&lt;/li&gt;
             &lt;li&gt;&lt;input type="button" name="list" value="" onclick="modelClick()"&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="clear"&gt;
        &lt;p style=""&gt;
          &lt;span&gt;DDD
           &lt;/span&gt;
        &lt;/p&gt;
        &lt;ul class="clear"&gt;
         &lt;li&gt;&lt;input type="button" name="select" value="" onclick="listClick();"&gt;&lt;/li&gt;
           &lt;li&gt;&lt;input type="button" name="list" value="" onclick="modelClick()"&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  </code></pre>