-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlearn_selenium.py
39 lines (34 loc) · 1.21 KB
/
learn_selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q") # 根据name查找元素
elem.send_keys("pycon") # 输入内容
elem.send_keys(Keys.RETURN) # 回车
print(driver.page_source) # 返回浏览器渲染后页面
# 为页面添加Cookies
driver.get("http://www.python.org")
cookie = {'name': 'foo', 'value': 'bar'}
driver.add_cookie(cookie)
# 获取页面Cookies,用法如下
driver.get("http://www.python.org")
driver.get_cookies()
# 页面等待
# 显式等待
driver = webdriver.Chrome()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
# 隐式等待
driver = webdriver.Chrome()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")