selenium的使用
零溪北、獲取chrome
http://chromedriver.storage.googleapis.com/index.html
https://github.com/mozilla/geckodriver/releases
一、導(dǎo)入selenium,開(kāi)始使用selenium
from selenium import webdriver
driver = webdriver.Chrome()
d = driver.get('https://www.baidu.com')
二缠犀、獲取元素
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
三魄缚、發(fā)送信息send_keys
elem.send_keys("美女")
四葬毫、清空信息clear
elem.clear()
五挖腰、點(diǎn)擊click
elem_btn.click()
四、關(guān)閉瀏覽器
driver.close()
五佛掖、js驅(qū)動(dòng)
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
for i in range(10):
time.sleep(3)
end = start + 700
js = f'window.scrollTo({start}, {end})'
driver.execute_script(js)
start += 700
六妖碉、獲取文本
elem.text
七、隱式時(shí)間等待
driver.implicitly_wait(10)
八芥被、瀏覽器的前進(jìn)和后退
back()
forward()
九欧宜、cookie操作
get_cookies()
delete_all_cookes()
add_cookie()
browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'zhaofan'})
十、選項(xiàng)卡切換
print(driver.window_handles)
driver.execute_script('window.open()')
driver.switch_to_window(driver.window_handles[1])
driver.get('https://www.taobao.com')
time.sleep(1)
driver.switch_to_window(driver.window_handles[0])
driver.get('https://python.org')