一、Selenium 的基本方法使用
from selenium
import webdriver
import time
browser
= webdriver
.Chrome
()
browser
.get
("http://www.baidu.com")
browser
.back
()
browser
.forward
()
browser
.refresh
()
print(browser
.current_url
)
print(browser
.title
)
print(browser
.page_source
)
print(browser
.window_handles
)
print(browser
.current_window_handle
)
二、selenium 元素定位
1. 根据ID属性进行定位,查询到的是一个WebElement对象(find_element_by_id)
好处:id 是唯一的
from selenium
import webdriver
import time
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
web01
= driver
.find_element_by_id
("kw")
web01
.send_keys
("天气预报")
time
.sleep
(3)
driver
.quit
()
2. 根据class属性进行定位(find_element_by_class_name)
注意:不能有空格
from selenium
import webdriver
import time
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
web01
= driver
.find_element_by_class_name
("s_ipt")
web01
.send_keys
("中国")
time
.sleep
(3)
driver
.quit
()
3. 根据name属性进行定位(find_element_by_name)
from selenium
import webdriver
import time
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
web01
= driver
.find_element_by_name
("wd")
web01
.send_keys
("美国")
time
.sleep
(3)
driver
.quit
()
4. 通过tagname(标签名称)进行定位。查找到的元素不具备唯一性(find_elements_by_tag_name)
```python
from selenium
import webdriver
import time
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
we04
= browser
.find_elements_by_tag_name
("input")
print(we04
[2])
5. 定位a标签超链接(find_element_by_link_text)
from selenium
import webdriver
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
web01
= driver
.find_element_by_link_text
("地图")
web01
.click
()
6. 定位a标签超链接,模糊定位(find_elements_by_partial_link_text)
from selenium
import webdriver
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
we04
= driver
.find_element_by_partial_link_text
("新")
we04
.click
()
7. 通过xpath定位元素
from selenium
import webdriver
driver
= webdriver
.Chrome
()
driver
.get
('http://www.baidu.com')
web04
= driver
.find_element_by_xpath
("//*[@id='kw']")
web04
.send_keys
("python")
8. 通过css 定位元素
from selenium
import webdriver
driver
= webdriver
.Chrome
()
driver
.get
('http://www.baidu.com')
'''
# 代表 id
'''
'''
. 代表 class
'''
web02
= driver
.find_element_by_css_selector
("input.s_ipt")
web02
.send_keys
("天天向上")
三、find_element_by...和find_elements_by...的区别
find_elelment_by...如果找到多个元素,只会返回 WebElement 对象find_elements_by...不管找到多少个,都会放回一个list
from selenium
import webdriver
driver
= webdriver
.Chrome
(executable_path
=r
"F:\python\chromedriver.exe")
driver
.get
("http://www.baidu.com")
web01
= driver
.find_elements_by_tag_name
("input")
print(type(web01
))
web02
= driver
.find_element_by_tag_name
("input")
print(type(web02
))
'''
<class 'list'>
<class 'selenium.webdriver.remote.webelement.WebElement'>
Process finished with exit code 0
'''