[TOC]
1 2 3 4 5 6 7 8 9 driver.find_element(By.CLASS_NAME,"xx" ) driver.find_element(By.CSS_SELECTOR,"xx" ) driver.find_element(By.ID,"xx" ) driver.find_element(By.LINK_TEXT,"xx" ) driver.find_element(By.NAME,"xx" ) driver.find_element(By.PARITIAL_LINK_TEXT,"xx" ) driver.find_element(By.TAG_NAME,"xx" ) driver.find_element(By.XPATH,"xx" )
环境准备 python版本: python3.8
系统: macOS 12.6
Selenium版本: selenium-4.7.2
python安装 brew install python@3.8
selenium 安装:
pip3 install selenium-4.7.2
pip3 install webdriver-manager # 浏览器驱动
反爬虫配置 1 2 3 4 5 6 7 from selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.action_chains import ActionChainsoption = webdriver.ChromeOptions() option.add_argument("--disable-blink-features=AutomationControlled" ) option.add_experimental_option('exculdeSwitches' , ['enable-automation' ])
实例化浏览器对象 驱动下载地址 https://chromedriver.chromium.org/downloads 请于本机电脑浏览器版本一致
1 2 3 4 driver = Service(r"/opt/chromedriver" ) browser = webdriver.Chrome(service=driver)
Class Name 定位 Class name 定位更容易一些
class=”ng-binding active” classs属性中的空格,表示此class属性有多个属性,且相互独立,使用class name定位此元素,只能是其中一个属性名 且唯一
属性 ng-binding active 都不是唯一 报错
例如
1 2 3 4 5 6 7 8 <a id ="helo" class ="ng-binding active" /a> task_list = browser.find_element(By.CLASS_NAME, "ng-binding active" ) time.sleep(3 ) task_list = browser.find_element(By.PARTIAL_LINK_TEXT, "任务管理" ).click()
获取下拉列表(非select标签) 思路:获取所有的下拉值, 在选择自己需要的选项 通过find.elements获取所有class name 相同的值,在选择需要的选项。
class name 总计有7个 需要获取第二个。
1 2 3 4 5 6 7 8 9 10 values = [browser.find_elements(By.CLASS_NAME, "ant-select-arrow" )]for value in values: value[2 ].click() print ("点击下拉框的下拉按钮" ) ele_lists = [browser.find_elements(By.CLASS_NAME, 'ant-select-dropdown-menu-item' )]for ele in ele_lists: print (ele[1 ].text) error = ele[1 ].click()
单个class name 获取title属性值
1 2 3 4 5 6 ele = browser.find_element(By.CSS_SELECTOR, "[class='ant-select-selection-selected-value']" ) # ele = browser.find_element(By.CLASS_NAME, "ant-select-selection-selected-value" ) print(print(ele.get_attribute("title" ) ) # xpath 路径可在网页复制 相对路径 # elx = browser.find_element(By.XPATH, '/ / * [@id ="task-list-page" ]/ div / div [2]/ div [2]/ div / div [1]/ div [1]/ div [3]/ div / div [1]/ div / div / div ')
鼠标操作 1 2 3 4 5 6 7 8 from selenium.webdriver import ActionChains driver = Service(r"/opt/chromedriver" ) browser = webdriver.Chrome(service=driver) action = ActionChains(browser) action.move_to_element(error).click()