00015 selenium webdriver簡單示例

https://chromedriver.chromium.org/getting-startedweb

Getting started

Sample test

import time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

 

import time
from selenium import webdriver

driver = webdriver.Chrome(r'C:\Users\user1\chromedriver_win32\chromedriver.exe')
driver.get('https://www.baidu.com/');
time.sleep(5)
search_box = driver.find_element_by_name('wd')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()

 

Controlling ChromeDriver's lifetime

The ChromeDriver class starts the ChromeDriver server process at creation and terminates it when quit is called. This can waste a significant amount of time for large test suites where a ChromeDriver instance is created per test.chrome

Use the ChromeDriverService. This is available for most languages and allows you to start/stop the ChromeDriver server yourself.ui

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service('/path/to/chromedriver')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
driver.quit()
相關文章
相關標籤/搜索