blob: 55410e566719e7e238a6aa209f6456576553d945 [file] [log] [blame] [edit]
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="en">
<meta name="lang:search.pipeline.stopwords" content="True">
<meta name="lang:search.pipeline.trimmer" content="True">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">
<link href="https://fonts.gstatic.com/" rel="preconnect" crossorigin>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,500,700|Roboto:300,400,400i,700&display=fallback" rel="stylesheet">
<style>
body,
input {
font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif
}
code,
kbd,
pre {
font-family: "Roboto Mono", "Courier New", Courier, monospace
}
</style>
<link rel="stylesheet" href="_static/stylesheets/application.css"/>
<link rel="stylesheet" href="_static/stylesheets/application-palette.css"/>
<link rel="stylesheet" href="_static/stylesheets/application-fixes.css"/>
<link rel="stylesheet" href="_static/fonts/material-icons.css"/>
<meta name="theme-color" content="#3f51b5">
<script src="_static/javascripts/modernizr.js"></script>
<title>Selenium Client Driver &#8212; Selenium 4.36.0 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="_static/material.css?v=79c92029" />
<script src="_static/documentation_options.js?v=48fdbe4b"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
</head>
<body dir=ltr
data-md-color-primary=blue-grey data-md-color-accent=blue>
<svg class="md-svg">
<defs data-children-count="0">
<svg xmlns="http://www.w3.org/2000/svg" width="416" height="448" viewBox="0 0 416 448" id="__github"><path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z"/></svg>
</defs>
</svg>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<a href="#index" tabindex="1" class="md-skip"> Skip to content </a>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex navheader">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="#" title="Selenium 4.36.0 documentation"
class="md-header-nav__button md-logo">
&nbsp;
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">Selenium 4.36.0 documentation</span>
<span class="md-header-nav__topic"> Selenium Client Driver </span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" action="search.html" method="get" name="search">
<input type="text" class="md-search__input" name="q" placeholder=""Search""
autocapitalize="off" autocomplete="off" spellcheck="false"
data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
&#xE5CD;
</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="_static/javascripts/version_dropdown.js"></script>
<script>
var json_loc = ""versions.json"",
target_loc = "../",
text = "Versions";
$( document ).ready( add_version_dropdown(json_loc, target_loc, text));
</script>
</div>
</nav>
</header>
<div class="md-container">
<nav class="md-tabs" data-md-component="tabs">
<div class="md-tabs__inner md-grid">
<ul class="md-tabs__list">
<li class="md-tabs__item"><a href="#" class="md-tabs__link">Selenium 4.36.0 documentation</a></li>
</ul>
</div>
</nav>
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0">
<label class="md-nav__title md-nav__title--site" for="__drawer">
<a href="#" title="Selenium 4.36.0 documentation" class="md-nav__button md-logo">
<img src="_static/" alt=" logo" width="48" height="48">
</a>
<a href="#"
title="Selenium 4.36.0 documentation">Selenium 4.36.0 documentation</a>
</label>
</nav>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">"Contents"</label>
<ul class="md-nav__list" data-md-scrollfix="">
<li class="md-nav__item"><a href="#index--page-root" class="md-nav__link">Selenium Client Driver</a><nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item"><a href="#introduction" class="md-nav__link">Introduction</a>
</li>
<li class="md-nav__item"><a href="#supported-python-versions" class="md-nav__link">Supported Python Versions</a>
</li>
<li class="md-nav__item"><a href="#supported-browsers" class="md-nav__link">Supported Browsers</a>
</li>
<li class="md-nav__item"><a href="#installing" class="md-nav__link">Installing</a>
</li>
<li class="md-nav__item"><a href="#drivers" class="md-nav__link">Drivers</a>
</li>
<li class="md-nav__item"><a href="#example-0" class="md-nav__link">Example 0:</a>
</li>
<li class="md-nav__item"><a href="#example-1" class="md-nav__link">Example 1:</a>
</li>
<li class="md-nav__item"><a href="#example-2" class="md-nav__link">Example 2:</a>
</li>
<li class="md-nav__item"><a href="#selenium-grid-optional" class="md-nav__link">Selenium Grid (optional)</a>
</li>
<li class="md-nav__item"><a href="#use-the-source-luke" class="md-nav__link">Use The Source Luke!</a>
</li>
<li class="md-nav__item"><a href="#contributing" class="md-nav__link">Contributing</a>
</li></ul>
</nav>
</li>
<li class="md-nav__item"><a class="md-nav__extra_link" href="_sources/index.rst.txt">Show Source</a> </li>
<li id="searchbox" class="md-nav__item"></li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset" role="main">
<section id="selenium-client-driver">
<h1 id="index--page-root">Selenium Client Driver<a class="headerlink" href="#index--page-root" title="Link to this heading"></a></h1>
<section id="introduction">
<h2 id="introduction">Introduction<a class="headerlink" href="#introduction" title="Link to this heading"></a></h2>
<p>Python language bindings for Selenium WebDriver.</p>
<p>The <cite>selenium</cite> package is used to automate web browser interaction from Python.</p>
<table>
<tbody>
<tr class="row-odd"><td><p><strong>Home</strong>:</p></td>
<td><p><a class="reference external" href="https://selenium.dev">https://selenium.dev</a></p></td>
</tr>
<tr class="row-even"><td><p><strong>GitHub</strong>:</p></td>
<td><p><a class="reference external" href="https://github.com/SeleniumHQ/Selenium">https://github.com/SeleniumHQ/Selenium</a></p></td>
</tr>
<tr class="row-odd"><td><p><strong>PyPI</strong>:</p></td>
<td><p><a class="reference external" href="https://pypi.org/project/selenium">https://pypi.org/project/selenium</a></p></td>
</tr>
<tr class="row-even"><td><p><strong>IRC/Slack</strong>:</p></td>
<td><p><a class="reference external" href="https://www.selenium.dev/support/#ChatRoom">https://www.selenium.dev/support/#ChatRoom</a></p></td>
</tr>
<tr class="row-odd"><td><p><strong>Docs</strong>:</p></td>
<td><p><a class="reference external" href="https://www.selenium.dev/selenium/docs/api/py">https://www.selenium.dev/selenium/docs/api/py</a></p></td>
</tr>
<tr class="row-even"><td><p><strong>API Reference</strong>:</p></td>
<td><p><a class="reference external" href="https://www.selenium.dev/selenium/docs/api/py/api.html">https://www.selenium.dev/selenium/docs/api/py/api.html</a></p></td>
</tr>
</tbody>
</table>
<p>Updated documentation published with each commit is available at: <a class="reference external" href="https://selenium-python-api-docs.readthedocs.io/en/latest">readthedocs.io</a></p>
</section>
<hr class="docutils"/>
<section id="supported-python-versions">
<h2 id="supported-python-versions">Supported Python Versions<a class="headerlink" href="#supported-python-versions" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>Python 3.9+</p></li>
</ul>
</section>
<section id="supported-browsers">
<h2 id="supported-browsers">Supported Browsers<a class="headerlink" href="#supported-browsers" title="Link to this heading"></a></h2>
<p>Several browsers are supported, as well as the Remote protocol:</p>
<ul class="simple">
<li><p>Chrome</p></li>
<li><p>Edge</p></li>
<li><p>Firefox</p></li>
<li><p>Safari</p></li>
<li><p>WebKitGTK</p></li>
<li><p>WPEWebKit</p></li>
</ul>
</section>
<section id="installing">
<h2 id="installing">Installing<a class="headerlink" href="#installing" title="Link to this heading"></a></h2>
<p>Install or upgrade the Python bindings with <cite>pip &lt;https://pip.pypa.io/&gt;</cite>.</p>
<p>Latest official release:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="o">-</span><span class="n">U</span> <span class="n">selenium</span>
</pre></div>
</div>
<p>Nightly development release:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="o">-</span><span class="n">U</span> <span class="o">--</span><span class="n">index</span><span class="o">-</span><span class="n">url</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">test</span><span class="o">.</span><span class="n">pypi</span><span class="o">.</span><span class="n">org</span><span class="o">/</span><span class="n">simple</span><span class="o">/</span> <span class="o">--</span><span class="n">extra</span><span class="o">-</span><span class="n">index</span><span class="o">-</span><span class="n">url</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">pypi</span><span class="o">.</span><span class="n">org</span><span class="o">/</span><span class="n">simple</span><span class="o">/</span> <span class="n">selenium</span>
</pre></div>
</div>
<p>Note: you should consider using a
<a class="reference external" href="https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments">virtual environment</a>
to create an isolated Python environment for installation.</p>
</section>
<section id="drivers">
<h2 id="drivers">Drivers<a class="headerlink" href="#drivers" title="Link to this heading"></a></h2>
<p>Selenium requires a driver to interface with the chosen browser (chromedriver, edgedriver, geckodriver, etc).</p>
<p>In older versions of Selenium, it was necessary to install and manage these drivers yourself. You had to make sure the
driver executable was available on your system <cite>PATH</cite>, or specified explicitly in code. Modern versions of Selenium
handle browser and driver installation for you with
<a class="reference external" href="https://www.selenium.dev/documentation/selenium_manager">Selenium Manager</a>. You generally don’t have to worry about
driver installation or configuration now that it’s done for you when you instantiate a WebDriver. Selenium Manager works
with most supported platforms and browsers. If it doesn’t meet your needs, you can still install and specify browsers
and drivers yourself.</p>
<p>Links to some of the more popular browser drivers:</p>
<table>
<tbody>
<tr class="row-odd"><td><p><strong>Chrome</strong>:</p></td>
<td><p><a class="reference external" href="https://developer.chrome.com/docs/chromedriver">https://developer.chrome.com/docs/chromedriver</a></p></td>
</tr>
<tr class="row-even"><td><p><strong>Edge</strong>:</p></td>
<td><p><a class="reference external" href="https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver">https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver</a></p></td>
</tr>
<tr class="row-odd"><td><p><strong>Firefox</strong>:</p></td>
<td><p><a class="reference external" href="https://github.com/mozilla/geckodriver">https://github.com/mozilla/geckodriver</a></p></td>
</tr>
<tr class="row-even"><td><p><strong>Safari</strong>:</p></td>
<td><p><a class="reference external" href="https://webkit.org/blog/6900/webdriver-support-in-safari-10">https://webkit.org/blog/6900/webdriver-support-in-safari-10</a></p></td>
</tr>
</tbody>
</table>
</section>
<section id="example-0">
<h2 id="example-0">Example 0:<a class="headerlink" href="#example-0" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>launch a new Chrome browser</p></li>
<li><p>load a web page</p></li>
<li><p>close the browser</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">selenium</span><span class="w"> </span><span class="kn">import</span> <span class="n">webdriver</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Chrome</span><span class="p">()</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'https://selenium.dev/'</span><span class="p">)</span>
<span class="n">driver</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="example-1">
<h2 id="example-1">Example 1:<a class="headerlink" href="#example-1" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>launch a new Chrome browser</p></li>
<li><p>load the Selenium documentation page</p></li>
<li><p>find the “Webdriver” link</p></li>
<li><p>click the “WebDriver” link</p></li>
<li><p>close the browser</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">selenium</span><span class="w"> </span><span class="kn">import</span> <span class="n">webdriver</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">selenium.webdriver.common.by</span><span class="w"> </span><span class="kn">import</span> <span class="n">By</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Chrome</span><span class="p">()</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'https://selenium.dev/documentation'</span><span class="p">)</span>
<span class="k">assert</span> <span class="s1">'Selenium'</span> <span class="ow">in</span> <span class="n">driver</span><span class="o">.</span><span class="n">title</span>
<span class="n">elem</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">ID</span><span class="p">,</span> <span class="s1">'m-documentationwebdriver'</span><span class="p">)</span>
<span class="n">elem</span><span class="o">.</span><span class="n">click</span><span class="p">()</span>
<span class="k">assert</span> <span class="s1">'WebDriver'</span> <span class="ow">in</span> <span class="n">driver</span><span class="o">.</span><span class="n">title</span>
<span class="n">driver</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="example-2">
<h2 id="example-2">Example 2:<a class="headerlink" href="#example-2" title="Link to this heading"></a></h2>
<p>Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example using Python’s
standard <a class="reference external" href="http://docs.python.org/3/library/unittest.html">unittest</a> library:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">unittest</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">selenium</span><span class="w"> </span><span class="kn">import</span> <span class="n">webdriver</span>
<span class="k">class</span><span class="w"> </span><span class="nc">GoogleTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span><span class="w"> </span><span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">addCleanup</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">driver</span><span class="o">.</span><span class="n">quit</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_page_title</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'https://www.google.com'</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertIn</span><span class="p">(</span><span class="s1">'Google'</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">driver</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">(</span><span class="n">verbosity</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="selenium-grid-optional">
<h2 id="selenium-grid-optional">Selenium Grid (optional)<a class="headerlink" href="#selenium-grid-optional" title="Link to this heading"></a></h2>
<p>For local Selenium scripts, the Java server is not needed.</p>
<p>To use Selenium remotely, you need to also run a Selenium Grid. For information on running Selenium Grid:
<a class="reference external" href="https://www.selenium.dev/documentation/grid/getting_started/">https://www.selenium.dev/documentation/grid/getting_started/</a></p>
<p>To use Remote WebDriver see: <a class="reference external" href="https://www.selenium.dev/documentation/webdriver/drivers/remote_webdriver/?tab=python">https://www.selenium.dev/documentation/webdriver/drivers/remote_webdriver/?tab=python</a></p>
</section>
<section id="use-the-source-luke">
<h2 id="use-the-source-luke">Use The Source Luke!<a class="headerlink" href="#use-the-source-luke" title="Link to this heading"></a></h2>
<p>View source code online:</p>
<table>
<tbody>
<tr class="row-odd"><td><p><strong>Official</strong>:</p></td>
<td><p><a class="reference external" href="https://github.com/SeleniumHQ/selenium/tree/trunk/py">https://github.com/SeleniumHQ/selenium/tree/trunk/py</a></p></td>
</tr>
</tbody>
</table>
</section>
<section id="contributing">
<h2 id="contributing">Contributing<a class="headerlink" href="#contributing" title="Link to this heading"></a></h2>
<blockquote>
<div><ul class="simple">
<li><p>Fork the selenium repo</p></li>
<li><p>Clone your fork locally</p></li>
<li><dl class="simple">
<dt>Create a branch for your work</dt><dd><ul>
<li><p><cite>git checkout -b my-cool-branch-name</cite></p></li>
</ul>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>Create a virtual environment and install tox</dt><dd><ul>
<li><p><cite>python -m venv venv &amp;&amp; source venv/bin/activate &amp;&amp; pip install tox</cite></p></li>
</ul>
</dd>
</dl>
</li>
<li><p>Make your changes</p></li>
<li><dl class="simple">
<dt>Run the linter/formatter</dt><dd><ul>
<li><p><cite>tox -e linting</cite></p></li>
</ul>
</dd>
</dl>
</li>
<li><p>If tox exits <cite>0</cite>, commit and push. Otherwise, fix the newly introduced style violations</p></li>
<li><p>Submit a Pull Request</p></li>
</ul>
</div></blockquote>
</section>
</section>
</article>
</div>
</div>
</main>
</div>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
<div class="md-footer-copyright__highlight">
&#169; Copyright 2009-2025 Software Freedom Conservancy.
</div>
</div>
</div>
</div>
</footer>
<script src="_static/javascripts/application.js"></script>
<script>app.initialize({version: "1.0.4", url: {base: ".."}})</script>
</body>
</html>