各种定位元素 selnium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
#打开浏览器
driver=webdriver.Chrome()
url="https://www.baidu.com"
#加载网页
driver.get(url)
#定位元素(利于封装)
#用id
#ele=driver.find_element(By.ID_TEXT,"kw").click()
#用name
#ele=driver.find_element(By.NAME,"wd").click()
#用link_text
#ele=driver.find_element(By.LINK_TEXT,"新闻").click()
#用partail link
#ele=driver.find_element(By.PARTIAL_LINK_TEXT,"新").click()
#xpath
#绝对路径 /绝对路径 比如"/html/body/div[1]/div[1]/div[5]/div/div/form/span[2]/input" //相对路径 "//input"
#1.相对路径+索引定位
#ele=driver.find_element(By.XPATH,"//form/span[1]/input").send_keys("python")
#2.相对路径+属性定位
#ele=driver.find_element(By.XPATH,"//input[@autocomplete="off").send_keys("python")
#3.相对路径+通配符定位
ele=driver.find_element(By.XPATH,"//*[@id='kw']").send_keys("python")
#4.相对路径+部分属性值定位
以开头//input[starts-with(@autocomplete,'of')]
以结尾//input[substring(@autocomplete,2)='ff']从第二个开始截取
包含//*[contains(@autocomplete,'ff')]
#5.相对路径+文本定位 并打印出class中的值
value=driver.find_element(By.XPATH,"//span[text()='按图片搜索']").get_attribute('class');
print(value)
css



 

 



time.sleep(3)


driver.close()
posted @ 2023-03-19 00:01  胖豆芽  阅读(20)  评论(0编辑  收藏  举报