|
8863 | 8863 | on GitHub.</p></li></ol></div><div class=td-content style=page-break-before:always><h1 id=pg-9106a7ac709cadefdb6e4b92e9e3ceaf>2.10.1 - Understanding Common Errors</h1><div class=lead>How to get deal with various problems in your Selenium code.</div><h2 id=invalid-selector-exception>Invalid Selector Exception</h2><p>CSS and XPath Selectors are sometimes difficult to get correct.</p><h3 id=likely-cause>Likely Cause</h3><p>The CSS or XPath selector you are trying to use has invalid characters or an invalid query.</p><h3 id=possible-solutions>Possible Solutions</h3><p>Run your selector through a validator service:</p><ul><li><a href=http://csslint.net/>CSS Validator</a></li><li><a href=http://www.freeformatter.com/xpath-tester.html>xPath Validator</a></li></ul><p>Or use a browser extension to get a known good value:</p><ul><li><a href=https://selectorshub.com/selectorshub/>SelectorsHub</a></li></ul><h2 id=no-such-element-exception>No Such Element Exception</h2><p>The element can not be found at the exact moment you attempted to locate it.</p><h3 id=likely-cause-1>Likely Cause</h3><ul><li>You are looking for the element in the wrong place (perhaps a previous action was unsuccessful).</li><li>You are looking for the element at the wrong time (the element has not shown up in the DOM, yet)</li><li>The locator has changed since you wrote the code</li></ul><h3 id=possible-solutions-1>Possible Solutions</h3><ul><li>Make sure you are on the page you expect to be on, and that previous actions in your code completed correctly</li><li>Make sure you are using a proper <a href=https://www.selenium.dev/documentation/webdriver/waits/>Waiting Strategy</a></li><li>Update the locator with the browser’s devtools console or use a browser extension like:<ul><li><a href=https://selectorshub.com/selectorshub/>SelectorsHub</a></li></ul></li></ul><h2 id=stale-element-reference-exception>Stale Element Reference Exception</h2><p>An element goes stale when it was previously located, but can not be currently accessed.
|
8864 | 8864 | Elements do not get relocated automatically; the driver creates a reference ID for the element and
|
8865 | 8865 | has a particular place it expects to find it in the DOM. If it can not find the element
|
8866 |
| -in the current DOM, any action using that element will result in this exception.</p><h3 id=common-causes>Common Causes</h3><p>This can happen when:</p><ul><li>You have refreshed the page, or the DOM of the page has dynamically changed.</li><li>You have navigated to a different page.</li><li>You have switched to another window or into or out of a frame or iframe.</li></ul><h3 id=common-solutions>Common Solutions</h3><p><strong>The DOM has changed</strong></p><p>When the page is refreshed or items on the page have moved around, there is still |
| 8866 | +in the current DOM, any action using that element will result in this exception.</p><h3 id=likely-cause-2>Likely Cause</h3><p>This can happen when:</p><ul><li>You have refreshed the page, or the DOM of the page has dynamically changed.</li><li>You have navigated to a different page.</li><li>You have switched to another window or into or out of a frame or iframe.</li></ul><h3 id=possible-solutions-2>Possible Solutions</h3><p><strong>The DOM has changed</strong></p><p>When the page is refreshed or items on the page have moved around, there is still |
8867 | 8867 | an element with the desired locator on the page, it is just no longer accessible
|
8868 | 8868 | by the element object being used, and the element must be relocated before it can be used again.
|
8869 | 8869 | This is often done in one of two ways:</p><ul><li><p>Always relocate the element every time you go to use it. The likelihood of
|
|
8882 | 8882 | and have destroyed the context in which the element was located.
|
8883 | 8883 | You can’t just relocate it from the current context,
|
8884 | 8884 | and you can’t switch back to an active context where it is valid. If this is the reason
|
8885 |
| -for your error, you must both navigate back to the correct location and relocate it.</p><h2 id=invalid-sessionid-exception>Invalid SessionId Exception</h2><p>Sometimes the session you’re trying to access is different than what’s currently available</p><h3 id=likely-cause-2>Likely Cause</h3><p>This usually occurs when the session has been deleted (e.g. <code>driver.quit()</code>) or if the session has changed, like when the last tab/browser has closed (e.g. <code>driver.close()</code>)</p><h3 id=possible-solutions-2>Possible Solutions</h3><p>Check your script for instances of <code>driver.close()</code> and <code>driver.quit()</code>, and any other possible causes of closed tabs/browsers. It could be that you are locating an element before you should/can.</p></div><div class=td-content style=page-break-before:always><h1 id=pg-5a7d28a8d143d2eaa2a6727d561e2164>2.10.1.1 - Unable to Locate Driver Error</h1><div class=lead>Troubleshooting missing path to driver executable.</div><p>Historically, this is the most common error beginning Selenium users get |
| 8885 | +for your error, you must both navigate back to the correct location and relocate it.</p><h2 id=elementclickinterceptedexception>ElementClickInterceptedException</h2><p>This exception occurs when Selenium tries to click an element, but the click would instead be received |
| 8886 | +by a different element. Before Selenium will click an element, it checks if the element is visible, |
| 8887 | +unobscured by any other elements, and enabled - if the element is obscured, it will raise this exception.</p><h3 id=likely-cause-3>Likely Cause</h3><p><strong>UI Elements Overlapping</strong></p><p>Elements on the UI are typically placed next to each other, but occasionally elements may overlap. For example, |
| 8888 | +a navbar always staying at the top of your window as you scroll a page. If that navbar happens to be covering |
| 8889 | +an element we are trying to click, Selenium might believe it to be visible and enabled, but when you try to click |
| 8890 | +it will throw this exception. Pop-ups and Modals are also common offenders here.</p><p><strong>Animations</strong></p><p>Elements with animations have the potential to cause this exception as well - it is recommended to wait for |
| 8891 | +animations to cease before attempting to click an element.</p><h3 id=possible-solutions-3>Possible Solutions</h3><p><strong>Use Explicit Waits</strong></p><p><a href=https://www.selenium.dev/documentation/webdriver/waits/>Explicit Waits</a> will likely be your best friend in these instances. |
| 8892 | +A great way is to use <code>ExpectedCondition.ToBeClickable()</code> with <code>WebDriverWait</code> to wait until the right moment.</p><p><strong>Scroll the Element into View</strong></p><p>In instances where the element is out of view, but Selenium still registers the element as visible |
| 8893 | +(e.g. navbars overlapping a section at the top of your screen), you can use the <code>WebDriver.executeScript()</code> |
| 8894 | +method to execute a javascript function to scroll (e.g. <code>WebDriver.executeScript('window.scrollBy(0,-250)')</code>) |
| 8895 | +or you can utilize the Actions class with <code>Actions.moveToElement(element)</code>.</p><h2 id=invalid-sessionid-exception>Invalid SessionId Exception</h2><p>Sometimes the session you’re trying to access is different than what’s currently available</p><h3 id=likely-cause-4>Likely Cause</h3><p>This usually occurs when the session has been deleted (e.g. <code>driver.quit()</code>) or if the session has changed, |
| 8896 | +like when the last tab/browser has closed (e.g. <code>driver.close()</code>)</p><h3 id=possible-solutions-4>Possible Solutions</h3><p>Check your script for instances of <code>driver.close()</code> and <code>driver.quit()</code>, and any other possible causes |
| 8897 | +of closed tabs/browsers. It could be that you are locating an element before you should/can.</p></div><div class=td-content style=page-break-before:always><h1 id=pg-5a7d28a8d143d2eaa2a6727d561e2164>2.10.1.1 - Unable to Locate Driver Error</h1><div class=lead>Troubleshooting missing path to driver executable.</div><p>Historically, this is the most common error beginning Selenium users get |
8886 | 8898 | when trying to run code for the first time:</p><ul class="nav nav-tabs" id=tabs-0 role=tablist><li class=nav-item><button class="nav-link active" id=tabs-00-00-tab data-bs-toggle=tab data-bs-target=#tabs-00-00 role=tab data-td-tp-persist=java aria-controls=tabs-00-00 aria-selected=true>
|
8887 | 8899 | Java</button></li><li class=nav-item><button class=nav-link id=tabs-00-01-tab data-bs-toggle=tab data-bs-target=#tabs-00-01 role=tab data-td-tp-persist=python aria-controls=tabs-00-01 aria-selected=false>
|
8888 | 8900 | Python</button></li><li class=nav-item><button class=nav-link id=tabs-00-02-tab data-bs-toggle=tab data-bs-target=#tabs-00-02 role=tab data-td-tp-persist=csharp aria-controls=tabs-00-02 aria-selected=false>
|
|
14324 | 14336 | <label><a href=https://github.com/diemol>@diemol</a></label>
|
14325 | 14337 | <span class=contributions>538 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/23253546?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle>
|
14326 | 14338 | <label><a href=https://github.com/VietND96>@VietND96</a></label>
|
14327 |
| -<span class=contributions>245 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/3331063?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle> |
| 14339 | +<span class=contributions>246 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/3331063?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle> |
14328 | 14340 | <label><a href=https://github.com/selenium-ci>@selenium-ci</a></label>
|
14329 | 14341 | <span class=contributions>159 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/2972876?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle>
|
14330 | 14342 | <label><a href=https://github.com/ddavison>@ddavison</a></label>
|
|
14530 | 14542 | <label><a href=https://github.com/titusfortner>@titusfortner</a></label>
|
14531 | 14543 | <span class=contributions>232 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/in/2740?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle>
|
14532 | 14544 | <label><a href=https://github.com/apps/renovate>@renovate[bot]</a></label>
|
14533 |
| -<span class=contributions>133 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/3264250?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle> |
| 14545 | +<span class=contributions>135 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/3264250?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle> |
14534 | 14546 | <label><a href=https://github.com/alaahong>@alaahong</a></label>
|
14535 | 14547 | <span class=contributions>115 commits</span></div><div style=padding:10px><img src="https://avatars.githubusercontent.com/u/10705590?v=4" class=inline width=100 height=100 style=height:100px;height:100px;margin-bottom:.25em;vertical-align:middle>
|
14536 | 14548 | <label><a href=https://github.com/pujagani>@pujagani</a></label>
|
|
0 commit comments