driver.find_element(By.ID, "cheese")
(可以在上一步结果的基础上进一步缩小范围)mucho_cheese = driver.find_elements_by_css_selector("#cheese li")
driver.find_element(By.NAME, "name").send_keys(name)
ActionChains(driver).drag_and_drop(source, target).perform()
driver.find_element(By.CSS_SELECTOR, "input[type='submit']").click()
driver = Chrome()
driver.get("https://selenium.dev")
driver.current_url
driver.back()
driver.forward()
driver.refresh()
driver.title
driver.current_window_handle
driver.quit()
Chrome Driver
ChromeDriver是配合Selenium库使用的,因为只有安装ChromDriver,才能驱动Chrome浏览器完成响应的操作。
# 拷贝
cp chromedriver-mac-arm64/chromedriver /usr/local/bin
# 授权
cd /usr/local/bin
xattr -d com.apple.quarantine chromedriver
from selenium import webdriver
driver=webdriver.Chrome() #启动谷歌浏览器
driver.get("http://www.zhihu.com") #访问一个网页
driver.quit() #退出浏览器
from selenium import webdriver
url = "https://movie.douban.com/top250"
# Todo: 获取浏览器对象
browser = webdriver.Chrome()
browser.maximize_window()
browser.get(url)
# print(browser.page_source)
# Todo:搜索(输入+点击+重新输入)
# 查找并输入搜索查询
search_input = browser.find_element("css selector", "#inp-query")
search_input.send_keys("周星驰")
# 点击搜索按钮
search_button = browser.find_element("css selector", "#db-nav-movie > div.nav-wrap > div > div.nav-search > form > fieldset > div.inp-btn")
search_button.click()
# 在页面更改后重新查找搜索输入框
search_input = browser.find_element("css selector", "#inp-query")
# todo: 清除搜索输入框
search_input.clear()
# # 方法一:双击
# ActionChains(browser).double_click(search_input).perform()
# # 方法二:全选
# search_input.send_keys(Keys.CONTROL, "a")
# search_input.send_keys(Keys.DELETE)
# 方法三:js
# browser.execute_script("arguments[0].value = '';", search_input)
# 输入新的搜索查询
search_input.send_keys("刘德华")
# 再次点击搜索按钮
search_button = browser.find_element("css selector", "#db-nav-movie > div.nav-wrap > div > div.nav-search > form > fieldset > div.inp-btn")
search_button.click()
# 关闭浏览器窗口
browser.quit()
from selenium import webdriver
import csv
import time
url="https://www.jd.com/"
# 1.获取浏览器对象
browser=webdriver.Chrome()
# 2.窗口 最大化
browser.maximize_window()
# 3.请求url
browser.get(url)
# 4.获取文本框并赋值
browser.find_element("css selector","#key").send_keys("男士衣服")
# 5.找到搜索按钮
browser.find_element("css selector","#search > div > div.form > button").click()
# 6.查找所有的li
#J_goodsList > ul > li
#J_goodsList > ul > li:nth-child(2)
#J_goodsList > ul > li:nth-child(3)
# 原因:数据加载时异步加载,所以要等待数据加载完之后再获取
# 单位秒,秒的具体取值跟网速有关系,
time.sleep(2)
li_List=browser.find_element("css selector","#J_goodsList > ul > li")
# 7.循环
i=1
for li in li_List:
# 8.获取价格
# selenium提取标签 文本的方法:标签.text
price=li.find_element_by_css_selector("div > div.p-price > strong > i").text
# 9.获取标题
title=li.find_element_by_css_selector("div > div.p-name.p-name-type-2 > a > em") .text
# 10.获取店铺
# selenium提取标签属性的方法:标签.get_attribute("属性名")
store=li.find_element_by_css_selector("div > div.p-shop > span > a").get_attribute("title")
# 11.将内容写入到csv文件中
with open("男士衣服.cvs","a",encoding="utf-8",newline="") as file:
csv.writer(file).writerow([price,title,store])
print("第{}条下载成功".format(i))
i+=1
import ddddocr
ocr = ddddocr.DdddOcr()
pngData = browser.find_element(By.CLASS_NAME,"SecurityCode_codeImg__390LE").screenshot_as_png
res = ocr.classification(pngData)
print(res)
yzm_input = browser.find_element(By.CLASS_NAME,"SecurityCode_inputBox__mD9hI")
yzm_input.send_keys(res)
yzm_button = browser.find_element(By.CSS_SELECTOR,"#indexLayout > div.IndexLayout_bodyContainer__mAURt > div > div:nth-child(6) > div > div.adm-mask-content > div > div.SecurityCode_footBox__5_k_V > div.SecurityCode_btnItem__uroav.SecurityCode_mainBtn__Kja7V")
yzm_button.click()