title: Python/Selenium - Getting the element after an element
body: <p>I need to close a popup window from the website itself by pressing the "X" window on the top right. Here is the shortened, relevant part of my code:</p>
  
  <pre><code>chromedriver = r'C:\Users\do\Desktop\chromedriver.exe'
  browser = webdriver.Chrome(chromedriver)
  url = 'Fake.com'
  
  browser.get(url) 
  browser.find_element_by_id('imgAttachmentsImg').click()
  # I need to close out the window after this
  </code></pre>
  
  <p>The issue is that there are no unique identifiers for the "X" button itself. However, the pop up itself does have a unique identifier. I just need to be able to flow it down to the X button.</p>
  
  <p>Info from the page:</p>
  
  <pre><code>1. V&lt;div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="attachmentsDialogOverview" aria-labelledby="ui-id-3" style="position: absolute; height: auto; width: auto; top: 239px; left: 102px; display: block;"&gt;
    2. &lt;div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"&gt;
      3. &lt;span id="ui-id-3" class="ui-dialog-title"&gt;Attachments&lt;/span&gt;
     4. V&lt;button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close"&gt;
         5. &lt;span class="ui-button-icon-primary ui-icon ui-icon-closethick"&lt;/span&gt;
         6. &lt;span class="ui-button-text"&gt;close&lt;/span&gt;&lt;/button&gt;
  </code></pre>
  
  <p>I am new to using Python and Selenium. I switched over from VBA and still don't exactly understand all the syntax yet, so I do make mistakes! I did find a bandaid solution by just having sendkeys press Escape. But I am trying to actually understand how to actually solve this. I am not sure what errors I made with my solution:</p>
  
  <pre><code>browser.find_element_by_xpath("//span[@id, 'ui-id-3']/following-sibling::button").click()
  </code></pre>
  
  <p>Questions</p>
  
  <ol>
  <li><p>Why did my solution not work?</p></li>
  <li><p>How would I locate "ui-id-3"(on line 3) and get to the element on Line 4 to click it?</p></li>
  <li><p>How would I locate "ui-id-3"(on line 3) and get to the element on <strong>Line 5</strong> to click it? (just so I know how to move across multiple elements)</p></li>
  </ol>
  
  <hr>
  
  <p>Relevant links I looked over:</p>
  
  <p><a href="https://stackoverflow.com/questions/41127120/following-sibling-python-selenium">Following-Sibling Python Selenium</a></p>
  
  <p><a href="https://stackoverflow.com/questions/23887592/find-next-sibling-element-in-python-selenium">Find next sibling element in Python Selenium?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8923721/using-xpath-selector-following-siblingtext-in-selenium-python">Using XPath Selector &#39;following-sibling::text()&#39; in Selenium (Python)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/30948860/python-selenium-webdriver-get-div-value-if-sibling-contains-string">Python + Selenium WebDriver - Get div value if sibling contains string</a></p>
  
  <p><a href="https://stackoverflow.com/questions/45697705/what-is-the-correct-syntax-for-using-a-variable-and-following-sibling-in-python">What is the correct syntax for using a variable and following-sibling in Python Selenium?</a></p>