css定位详解
html 提到css层叠样式,
格式:
find_element_by_css_selector(‘定位元素’)
案例:
driver.find_element_by_css_selector(’#kw’).send_keys(“css定位”)
========================================================
场景一:css中的id定位
有两种:(1)直接使用id值;(2)id简化方式
(1)直接使用id值
案例:
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(’[id=“kw”]’).send_keys(“css定位中的id值定位”)
注意点:id值需要加上[ ] 扩起来
(2)id简化方式(直接使用#+id值)
2.1 直接输入#id值
2.2在定位的元素中,右键,copy,copy selector 得到id的简化值
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(’#kw’).send_keys(“css定位中的id简化值定位”)
=====================================================
2.1一种是简化法:(.)点+class值
2.2直接输入[class属性名=值]
案例1:简化方法
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(’.s_ipt’).send_keys(“css定位中的class简化方法”)
案例:class直接输入方法
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(’[class=“s_ipt”]’).send_keys(“css定位中的class方法”)
=====================================================
场景三:css中name定位方法
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(’[name=“wd”]’).send_keys(“css定位中的name方法”)
======================================================
场景四:css中id组合属性定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(‘input#kw’).send_keys(“css定位中的id组合属性定位”)
======================================================
场景五:css中class组合定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(‘input.s_ipt’).send_keys(“css定位中的class组合属性定位”)
======================================================
场景六:css中name组合属性定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(‘input[name=“wd”]’).send_keys(“css定位中的name组合属性定位”)
======================================================
场景七:两个其他属性组合定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(’[name=“wd”][autocomplete=“off”]’).send_keys(“css定位中的两个其他属性定位”)
======================================================
场景八:
css中定位仅使用属性名,没有值定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(“input[id]”).send_keys(“css定位中仅有的属性名”)
======================================================
场景九:上定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(‘span>input’).send_keys(“上上级定位”)
======================================================
场景十:上上级定位
from selenium import webdriver
from time import sleep
driver=webdriver.Chrome()#通过对象谷歌浏览区且输入网址,在用get方法打开浏览区
url=“http://www.baidu.com/”
driver.get(url)
sleep(2)
driver.maximize_window()
sleep(3)
driver.find_element_by_css_selector(‘form>span>input’).send_keys(“上上级定位”)
================================================================
拓展知识一;
~ 表示多个空格
^ 匹配属性字符开头
$ 匹配属性结尾
================================================================
拓展点二:elements 是复数,
id :
name :
class :
tag :
link :
xpath:
css :
partial_link :