How do I use the selenium example in python

  • 2020-06-19 10:57:45
  • OfStack

Recently, I wrote an python gadget based on selenium to record the learning record. My environment is Ubuntu 14.04.4, Python 2.7,Chromium 49.0,ChromeDriver 2.16

selenium profile

selenium provides a common interface to simulate the user to operate the browser, such as for automated testing.

At the heart of selenium is WebDriver, which provides a set of interfaces that operate across a variety of browsers.

Major browser vendors also support Selenium as part 1 of their browsers.

The selenium toolset provides WebDriver,Selenium IDE, ES27en-ES28en, etc

Selenium 1.0 + WebDriver = Selenium 2.0

Selenium WebDriver is the successor to Selenium Remote Control(ES39en-ES40en).

WebDriver overcomes some of the limitations of ES43en-ES44en API1 by providing a simpler and cleaner interface. WebDriver is an object-oriented service compared to Selenium 1.0. WebDriver drives the browser more efficiently, providing more functionality than Selenium 1.0 Selenium RC runs only on stand-alone machines, while WebDriver provides the ability to operate remotely

selenium basic use

What does selenium need to run

There are three main parts :selenium selenium, Browser driver, browser selenium selenium are a set of common interfaces, while different browsers provide their own driver(most of which are official), and browsers are simulated to control the operation of the terminal.

The installation


pip install selenium --upgrade
apt-get install chromium-browser
wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux`getconf LONG_BIT`.zip
unzip chromedriver_linux32.zip
cp chromedriver /usr/local/share
chmod +x /usr/local/share/chromedriver
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver 
ln -s /usr/bin/chromedriver /usr/local/share/chromedriver 

Simple use


from selenium import webdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get('http://mail.sina.net');
print(driver.title)

API use

Reference/usr local lib/python2. 7 / dist - packages/selenium

Chrome WebDriver


selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None)

ChromeOptions

ChromeDriver session ChromeDriverconvenient methods for setting ES103en-ES104en capabilities can be configured via ChromeDriver session


from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-logging')
chrome_options.add_experimental_option('prefs', {'download.default_directory':
'/tmp'})
chrome_options.binary_location='/usr/bin/chromium-browser'
driver = webdriver.Chrome(chrome_options=chrome_options)

Use DesiredCapabilities directly

ChromeOptions is built on top of DesiredCapabilities, in order to use DesiredCapabilities, you must know the Key/value pair of capability.


chrome_options = Options()
capabilities={}
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
capabilities.update(chrome_options.to_capabilities())
driver = webdriver.Chrome(desired_capabilities=capabilities)

chromedriver operation mode

The ChromeDriver class can waste a lot of time by constantly creating instances, which can be solved in two ways.

Using ChromeDriverService


import selenium.webdriver.chrome.service as service
service = service.Service('/usr/bin/chromedrive')
service.start()
capabilities = { }
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://mail.sina.net');
print(driver.title)

Open a separate ChromeDriver service


./chromedriver
driver = webdriver.Remote('http://127.0.0.1:9515', DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');

RemoteWebDriverServer

The RemoteWebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a Java servlet, which can be hosted in any modern JEE app server. The server will always run on the machine with the browser you want to test.


wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
java -jar selenium-server-standalone-2.53.0.jar

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub',des
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');


Related articles: