ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```python from selenium import webdriver # 打开浏览器 # 或者将驱动放到Python的Scripts目录下,则可以写成 webdriver.Chrome() driver = webdriver.Chrome("D:/Drivers/ChromeDriver/chromedriver_win32/chromedriver.exe") # 向浏览器发送请求 driver.get('https://www.baidu.com') # 任务代码 # 退出浏览器 driver.quit() ```` <br/> **1. driver定位** ```python # 根据id定位元素 submitTag = driver.find_element_by_id('su') submitTag1 = driver.find_element(By.ID,'su') # 根据类选择器定位元素 submitTag = driver.find_element_by_class_name('su') submitTag1 = driver.find_element(By.CLASS_NAME,'su') # 根据name属性的值定位元素 submitTag = driver.find_element_by_name('email') submitTag1 = driver.find_element(By.NAME,'email') # 根据标签名定位元素 submitTag = driver.find_element_by_tag_name('div') submitTag1 = driver.find_element(By.TAG_NAME,'div') # 根据xpath语法定位元素 submitTag = driver.find_element_by_xpath('div') submitTag1 = driver.find_element(By.XPATH,'div') # 根据CSS选择器定位元素 submitTag = driver.find_element_by_css_selector('//div') submitTag1 = driver.find_element(By.CSS_SELECTOR,'//div') ``` **注意:** 上面列举的都是`find_element `函数,是获取第⼀个满⾜条件的元素;还有`find_elements` 是获取所有满⾜条件的元素。 ```python submitTags = driver.find_elements_by_css_selector('div') submitTag1s = driver.find_elements(By.CSS_SELECTOR,'div') ```