1.安装selenium库
pip install selenium
2.下载驱动
https://www.selenium.dev/zh-cn/documentation/webdriver/getting_started/install_drivers/
3.第一个小项目
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.google.com")
driver.title
driver.implicitly_wait(0.5)
search_box = driver.find_element(By.NAME, "q")
search_button = driver.find_element(By.NAME, "btnK")
search_box.send_keys("Selenium")
search_button.click()
driver.find_element(By.NAME, "q").get_attribute("value")
driver.quit()
4.浏览器元素
driver.title
driver.current_url
driver.get("https://selenium.dev")
driver.back()
driver.forward()
5.处理警告框,提示框和确认框
driver.find_element(By.LINK_TEXT, "See an example alert").click()
alert=wait.until(expected_conditions.alert_is_present())
text = alert.text
alert.accept()
driver.find_element(By.LINK_TEXT, "See a sample confirm").click()
wait.until(expected_conditions.alert_is_present())
alert = driver.switch_to.alert
text = alert.text
alert.dismiss()
driver.find_element(By.LINK_TEXT, "See a sample prompt").click()
wait.until(expected_conditions.alert_is_present())
alert = Alert(driver)
alert.send_keys("Selenium")
alert.accept()
6.处理cookies
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")
driver.add_cookie({"name": "key", "value": "value"})
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")
driver.add_cookie({"name": "foo", "value": "bar"})
print(driver.get_cookie("foo"))
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})
print(driver.get_cookies())
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})
driver.delete_cookie("test1")
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")
driver.add_cookie({"name": "test1", "value": "cookie1"})
driver.add_cookie({"name": "test2", "value": "cookie2"})
driver.delete_all_cookies()
7.iFrames
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
driver.switch_to.frame(iframe)
driver.find_element(By.TAG_NAME, 'button').click()
driver.switch_to.frame('buttonframe')
driver.find_element(By.TAG_NAME, 'button').click()
iframe = driver.find_elements_by_tag_name('iframe')[1]
driver.switch_to.frame(iframe)
driver.switch_to.default_content()
8.窗口和标签
driver.current_window_handle
driver.switch_to.new_window('tab')
driver.switch_to.new_window('window')
driver.close()
driver.switch_to.window(original_window)
driver.quit()
width = driver.get_window_size().get("width")
height = driver.get_window_size().get("height")
size = driver.get_window_size()
width1 = size.get("width")
height1 = size.get("height")
driver.set_window_size(1024, 768)
x = driver.get_window_position().get('x')
y = driver.get_window_position().get('y')
position = driver.get_window_position()
x1 = position.get('x')
y1 = position.get('y')
driver.set_window_position(0, 0)
driver.maximize_window()
driver.minimize_window()
driver.fullscreen_window()
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.example.com")
driver.save_screenshot('./image.png')
driver.quit()
header = driver.find_element(By.CSS_SELECTOR, "h1")
driver.execute_script('return arguments[0].innerText', header)
9.获取页面焦点
vegetable = driver.find_element(By.CLASS_NAME, "tomatoes")
fruits = driver.find_element(By.ID, "fruits")
fruit = fruits.find_elements_by_id("tomatoes")
fruit = driver.find_element_by_css_selector("#fruits .tomatoes")
plants = driver.find_elements(By.TAG_NAME, "li")
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("https://www.example.com")
elements = driver.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)
driver.get('https://www.example.com')
cssValue = driver.findElement(By.LINK_TEXT, "More information...").value_of_css_property('color')
fruit = driver.find_element_by_css_selector("#fruits .tomatoes")
10.总结
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.google.com")
driver.title
driver.implicitly_wait(0.5)
search_box = driver.find_element(By.NAME, "q")
search_button = driver.find_element(By.NAME, "btnK")
search_box.send_keys("Selenium")
search_button.click()
driver.find_element(By.NAME, "q").get_attribute("value")
driver.quit()
WebDriverWait(driver).until(document_initialised)
driver.implicitly_wait(0.5)
from selenium import webdriver
driver.save_screenshot('./image.png')
page=driver.page_source()
selenium官方教程 https://www.selenium.dev/zh-cn/documentation/webdriver/browser/
|