标签:row mon fresh button 选择 很多 https page comm
什么是selenium?
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.baidu.com'
browser.get(url)
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
from selenium.webdriver.common.by import By
# By的可用属性分别为:ID、XPATH、LINK_TEXT、PARTIAL_TEXT、NAME、CLASS_NAME、CSS_SELECTOR
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
from selenium import webdriver
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.Firefox()
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()
expected_conditions 模块提供了一组预定义的条件供WebDriverWait使用。
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
browser.find_element_by_id('kw').send_keys('Python')
browser.find_element_by_id('submit')
page_source # 页面源码
execute_script('js') # 执行js代码
fullscreen_window() # 浏览器全屏
get_screenshot_as_base64() # 截屏保存为base64格式
get_screenshot_as_file(filename) # 截屏保存为文件
get_screenshot_as_png() # 截屏保存为png
save_screenshot(filename) # 同上
quit() # 关闭浏览器
close() # 关闭当前Tab页面
refresh() # 刷新页面
switch_to_frame(frame_reference) # 切换到另一个frame
更多请查看:https://selenium-python-zh.readthedocs.io/en/latest/api.html
from selenium.webdriver.chrome.options import Options
# 创建一个参数对象,用来控制chrome以界面模式打开
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)
标签:row mon fresh button 选择 很多 https page comm
原文地址:https://www.cnblogs.com/tmdhhl/p/selenium.html