from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_id("kw").send_keys("id定位")
②name定位
python
1 2 3 4 5 6 7
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_name("wd").send_keys("name定位")
③class定位
python
1 2 3 4 5 6 7
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_class_name("s_ipt").send_keys("class定位")
④link_text 链接定位
python
1 2 3 4 5 6 7
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_link_text("hao123").click()
⑤ind_element_by_partial_link_text 模糊匹配
python
1 2 3 4 5 6 7
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_partial_link_text("hao").click()
⑥xpath定位 复制的xpath://*[@id=”kw”]
python
1 2 3 4 5 6 7
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_xpath('//*[@id="kw"]').send_keys("xpath定位")
⑦css定位 复制css :#kw
python
1 2 3 4 5 6 7
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() dx.find_element_by_css_selector("#kw").send_keys("css定位方法")
⑧js定位方法
python
1 2 3 4 5 6 7 8
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() js="document.getElementById('kw').value='js定位'" dx.execute_script(js)
⑨tag_name 标签名称定位
python
1 2 3 4 5 6 7 8 9 10
from selenium import webdriver from time import * dx=webdriver.Chrome() dx.get("https://www.baidu.com/") sleep(2) dx.maximize_window() inputs=dx.find_elements_by_tag_name("input") for i in inputs: if i.get_attribute("name")=="wd": i.send_keys("tag_name定位")