官方文檔 http://selenium-python.readthedocs.io/
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait browser = webdriver.Chrome() try: browser.get('https://www.baidu.com') inp = browser.find_element_by_id('kw') inp.send_keys('Python') inp.send_keys(Keys.ENTER) wait = WebDriverWait(browser, 10) wait.until(EC.presence_of_element_located((By.ID, 'content_left'))) print(browser.current_url) print(browser.get_cookies()) print(browser.page_source) except: pass finally: browser.close()
from selenium import webdriver browser = webdriver.Chrome() browser = webdriver.Firefox() browser = webdriver.Edge() browser = webdriver.PhantomJS() browser = webdriver.Safari()
browser = webdriver.Chrome() browser.get('https://www.taobao.ocm') print(browser.page_source) browser.close()
browser.get('https://www.taobao.com') inp_frist = browser.find_element_by_id('q') inp_second = browser.find_element_by_css_selector('#q') inp_thrid = browser.find_element_by_xpath('//*[@id="q"]') print(inp_frist, inp_second, inp_thrid) browser.close()
inp = browser.find_element(By.ID, 'q')
lis = browser.find_elements_by_css_selector('.service-bd li')
inp = browser.find_element_by_id('q') inp.send_keys('iPhone') time.sleep(1) inp.clear() inp.send_keys('iPad') button = browser.find_element_by_class_name('btn-search') button.click()
from selenium import webdriver from selenium.webdriver import ActionChains browser=webdriver.Chrome() url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) # 切換到 iframe browser.switch_to.frame('iframeResult') source = browser.find_element_by_css_selector('#draggable') target = browser.find_element_by_css_selector('#droppable') actions = ActionChains(browser) actions.drag_and_drop(source, target) actions.perform()
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore') browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') browser.execute_script('alert("To Bottom")')
from selenium import webdriver from selenium import ActionChains browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) logo = browser.find_element_by_id('zh-top-link-logo') print(logo) print(logo.get_attribute('class'))
browser.get(url) inp = browser.find_element_by_class_name('zu-top-add-question') print(inp.text) print(inp.id) print(inp.location) print(inp.tag_name) print(inp.size)
import time from selenium import webdriver from selenium.common import NoSuchElementException browser = webdriver.Chrome() url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult') source = browser.find_element_by_class_selector('#draggable') print(source) try: logo = browser.find_element_by_class_name('logo') except NoSuchElementException: print('no logo') browser.switch_to.parent_frame() logo = browser.find_element_by_class_name('logo') print(logo) print(logo.text)
當使用了隱式等待執行測試的時候,若是WebDriver沒有在DOM中找到元素,將繼續等待,超出設定時間後拋出找不到元素的異常,換句話說,當查找元素或元素沒有當即出現的
時候,隱式等待將等待一段時間再查找DOM,默認的時間0javascript
from selenium import webdriver browser = webdriver.Chrome() browser.implicitly_wait(10) browser.get('https://www.zhihu.com/explore') inp = browser.find_element_by_class_name('zu-top-add-question') print(inp)
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support expected_conditions as ec browser = webdriver.Chrome() browser.get('https://www.taobao.com') wait = WebDriverWait(browser, 10) inp = wait.until(ec.presence_of_element_located((By.ID, 'q'))) button = wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search'))) print('inp', button)
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.baidu.com') browser.get('https://www.taobao.com') browser.get('https://www.python.org') browser.back() browser.forward() browser.close()
browser.get_cookies() browser.add_cookie({'name':'y', 'domain': 'www.zhihu.com', 'value': 'g'}) browser.delete_all_cookies()
browser.get('https://www.baidu.com') browser.execute_script('window.open()') print(browser.window_handles) browser.get('https://www.taobao.com') browser.switch_to_window(browser.window_handles[1]) browser.get('https://python.org')
from selenium.common.exceptions import NoSuchElementException, TimeoutException