Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3687
Abstract
ments</span> = driver.find_elements(By.CLASS_NAME, <span class="hljs-string">"links"</span>)</pre></div><p id="c671">All <code>find_</code> methods can be called on both <code>WebDriver</code> and <code>WebElement</code><i> </i>instances that mean if you locate a container element by calling <code>find_</code> method on the <code>WebDriver</code> instance, you can call <code>find_*</code> on this element to further search elements inside it.</p><h1 id="582c">Example</h1><p id="9af8">The following example includes all location strategies and handling of <code>NoSuchElementException</code> case.</p>
<figure id="b42d">
<div>
<div>
<iframe class="gist-iframe" src="/gist/coskundeniz/984596dbec9ff900f987b7ab104051cb.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="ea96">You can also find the codes <a href="https://github.com/coskundeniz/selenium-examples/blob/main/locating_elements.py">here</a>.</p><div id="8e3b"><pre><span class="hljs-meta"># Output</span></pre></div><div id="afc8"><pre><span class="hljs-attr">Mail input field placeholder:</span> <span class="hljs-string">[email protected]</span>
<span class="hljs-attr">Submit button text:</span> <span class="hljs-string">SUBMIT</span> <span class="hljs-string">QUESTION</span>
<span class="hljs-attr">Second link text:</span> <span class="hljs-string">Hitchhiker’s</span> <span class="hljs-string">Guide</span> <span class="hljs-string">to</span> <span class="hljs-string">Python</span>
<span class="hljs-attr">Python Official link text:</span> <span class="hljs-string">Python</span> <span class="hljs-string">Official</span>
<span class="hljs-attr">Link text - 1:</span> <span class="hljs-string">Python</span> <span class="hljs-string">Official</span>
<span class="hljs-attr">Link text - 2:</span> <span class="hljs-string">Hitchhiker’s</span> <span class="hljs-string">Guide</span> <span class="hljs-string">to</span> <span class="hljs-string">Python</span>
<span class="hljs-attr">Link text - 3:</span> <span class="hljs-string">Dive</span> <span class="hljs-string">Into</span> <span class="hljs-string">Python</span>
<span class="hljs-attr">Link text - 4:</span> <span class="hljs-string">Real</span> <span class="hljs-string">Python</span>
<span class="hljs-attr">Link text - 5:</span> <span class="hljs-string">Python</span> <span class="hljs-string">Subreddit</span>
<span class="hljs-attr">Link text - 6:</span> <span class="hljs-string">Talk</span> <span class="hljs-string">Python</span> <span class="hljs-string">Podcast</span>
<span class="hljs-attr">Link text - 7:</span> <span class="hljs-string">Awesome</span> <span class="hljs-string">Python</span>
<span class="hljs-attr">Question text area placeholder:</span> <span class="hljs-string">Your</span> <span class="hljs-string">question...</span>
<span class="hljs-attr">Navigation item - 1:</span> <span class="hljs-string">About</span>
<span class="hljs-attr">Navigation item - 2:</span> <span class="hljs-string">BOPI</span>
<span class="hljs-attr">Navigation item - 3:</span> <span class="hljs-string">Feedback</span>
<span class="hljs-attr">Support button text:</span> <span class="hljs-string">Support</span> <span class="hljs-string">Me</span>
<span class="hljs-string">There</span> <span class="hljs-string">is</span> <span class="hljs-literal">no</span> <span class="hljs-string">h1</span> <span class="hljs-string">tag</span> <span class="hljs-string">in</span> <span class="hljs-string">the</span> <span class="hljs-string">document!</span></pre></div>
Options
<p id="e10e"><b>Tips on performance</b></p><ul><li>If an id is available for the element to be located, prefer using it. Since id must be unique on a page, the performance will be better.</li><li>If id is not available, prefer a well-written, targeted css selector.</li><li>Traversing the DOM is an expensive operation, so you should narrow the scope of your search as much as possible to get better performance.</li></ul><h2 id="6189">Things to Remember</h2><ul><li>You can locate elements by id attribute, name attribute, css selector, class name, tag name, xpath, and full or partial link text.</li><li>Selenium Python api provides <code>find_element(s)</code> and <code>find_element(s)_by_*</code> methods to locate elements on a page.</li><li>All <code>find_*</code> methods can be called on both <code>WebDriver</code> and <code>WebElement</code><i> </i>instances.</li><li>In case of failing to find, these methods return an empty list or raise <code>NoSuchElementException</code></li></ul><p id="b0ca">In the next post, I will write about waits in Selenium.</p><h2 id="d572">References</h2><ol><li><a href="https://www.selenium.dev/documentation/en/getting_started_with_webdriver/locating_elements/">https://www.selenium.dev/documentation/en/getting_started_with_webdriver/locating_elements/</a></li><li><a href="https://selenium-python.readthedocs.io/locating-elements.html">https://selenium-python.readthedocs.io/locating-elements.html</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors">https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors</a></li></ol><p id="41e6">Thank you for your time.</p><p id="2dae">If you want to read the previous posts, check out the following links.</p><div id="774a" class="link-block">
<a href="https://readmedium.com/browser-automation-with-python-and-selenium-3-architecture-fa01f7d0763a">
<div>
<div>
<h2>Browser Automation with Python and Selenium — 3: Architecture</h2>
<div><h3>Bird’s eye view of Selenium applications</h3></div>
<div><p>medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*YuBGTqKiy9f2mOPMbWg2QA.jpeg)"></div>
</div>
</div>
</a>
</div><div id="82f4" class="link-block">
<a href="https://codenineeight.medium.com/browser-automation-with-python-and-selenium-2-getting-started-708a6c17f2a3">
<div>
<div>
<h2>Browser Automation with Python and Selenium — 2: Getting Started</h2>
<div><h3>A simple example to start</h3></div>
<div><p>codenineeight.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*-dxo_DdJVaU4b8ZWhjac1g.jpeg)"></div>
</div>
</div>
</a>
</div><div id="fdc9" class="link-block">
<a href="https://codenineeight.medium.com/browser-automation-with-python-and-selenium-1-installation-1596d079d4d">
<div>
<div>
<h2>Browser Automation with Python and Selenium — 1: Installation</h2>
<div><h3>Towards to starting…</h3></div>
<div><p>codenineeight.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*FByORmFn6xQhccpL)"></div>
</div>
</div>
</a>
</div></article></body>