网站建设服务非常好湖南岚鸿公司,乡镇医院网站建设,设计素材网站会员哪个最好,网络推广常用工具Selenium4中有多种元素定位方式#xff0c;主要包括以下几种#xff1a;
通过ID属性定位#xff1a;根据元素的id属性进行定位。通过name属性定位#xff1a;当元素没有id属性而有name属性时#xff0c;可以使用name属性进行元素定位。通过class name定位#xff1a;可以…Selenium4中有多种元素定位方式主要包括以下几种
通过ID属性定位根据元素的id属性进行定位。通过name属性定位当元素没有id属性而有name属性时可以使用name属性进行元素定位。通过class name定位可以根据元素的class属性进行定位。通过tag name定位根据HTML标签名进行定位。通过link text定位根据链接的文本内容进行定位。通过partial link text定位根据链接的部分文本内容进行定位。通过css selector定位根据CSS选择器规则进行定位。通过xpath定位根据XPath表达式进行定位。
分别对应by方法的
class By:Set of supported locator strategies.ID idXPATH xpathLINK_TEXT link textPARTIAL_LINK_TEXT partial link textNAME nameTAG_NAME tag nameCLASS_NAME class nameCSS_SELECTOR css selector示例
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleepclass TestCase(object):def __init__(self):# 初始化方法创建一个Chrome浏览器实例并打开百度首页。self.driver webdriver.Chrome()self.driver.get(https://www.baidu.com/)self.driver.maximize_window() # 窗口最大化sleep(1)def test_id(self):通过ID定位搜索框输入selenium并点击搜索按钮。:return:element self.driver.find_element(byBy.ID, valuekw)element.send_keys(selenium)print(type(element))self.driver.find_element(byBy.ID, valuesu).click()sleep(3)# self.driver.quit()def test_name(self):通过NAME定位搜索框输入selenium并点击搜索按钮。:return:element self.driver.find_element(byBy.NAME, valuewd)element.send_keys(selenium)print(type(element))self.driver.find_element(byBy.ID, valuesu).click()sleep(3)self.driver.quit()def test_link_text(self):通过链接文本定位百度首页并点击。:return:self.test_id()self.driver.find_element(By.LINK_TEXT, value百度首页).click()sleep(3)self.driver.quit()def test_partiallink_text(self):通过部分链接文本定位百度首页并点击。:return:self.test_id()self.driver.find_element(By.PARTIAL_LINK_TEXT, value首页).click()sleep(3)self.driver.quit()def test_xpath(self):通过XPath定位搜索框输入selenium并点击搜索按钮。:return:element self.driver.find_element(byBy.XPATH, value//*[idkw])element.send_keys(selenium)print(type(element))self.driver.find_element(byBy.XPATH, value//*[idsu]).click()sleep(3)self.driver.quit()def test_tag(self):通过标签名定位input元素。:return:element self.driver.find_element(byBy.TAG_NAME, valueinput)print(element)def test_css_selector(self):通过CSS选择器定位搜索框输入selenium并点击搜索按钮。:return:element self.driver.find_element(byBy.CSS_SELECTOR, value#kw)element.send_keys(selenium)print(type(element))self.driver.find_element(byBy.XPATH, value//*[idsu]).click()sleep(3)self.driver.quit()def test_class_name(self):通过类名定位搜索框输入selenium并点击搜索按钮。:return: element self.driver.find_element(byBy.CLASS_NAME, values_ipt)element.send_keys(selenium)print(type(element))self.driver.find_element(byBy.XPATH, value//*[idsu]).click()sleep(3)self.driver.quit()if __name__ __main__:case TestCase()# case.test_id()# case.test_name()# case.test_link_text()# case.test_partiallink_text()# case.test_xpath()# case.test_tag()# case.test_css_selector()case.test_class_name()工具类封装及使用
from time import sleepfrom selenium import webdriver
from selenium.webdriver.common.by import Bydef get_element(driver,*loc):
# 使用find_element方法查找元素传入定位方式和定位值e driver.find_element(*loc)
# 返回找到的元素return eif __name__ __main__:driver webdriver.Chrome()driver.get(https://www.baidu.com/)driver.maximize_window()loc (By.ID, kw)get_element(driver,*loc).send_keys(selenium)sleep(10)
文章转载自: http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.mzhgf.cn.gov.cn.mzhgf.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.yrbqy.cn.gov.cn.yrbqy.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn