德胜云资讯,添加一些关于程序相关的内容,仅供大家学习交流(https://www.wxclwl.com)

网站地图

搜索
德胜云咨询
前端分类 javascript CSS 正则表达式 html 前端框架 typescript Ajax
热门标签:
最新标签:

css css3python学习之selenium~css定位完整版,附代码css hover的用法学到了吗,

日期:2023/03/27 14:16作者:李成白人气:

导读:css选择器有44种: .class: .intro,选择class=‘intro’的所有元素 #id: #firstname,选择id ......
css选择器有44种: .class: .intro,选择class=‘intro’的所有元素 #id: #firstname,选择id ...

css选择器有44种:

.class: .intro,选择class=‘intro’的所有元素

#id: #firstname,选择id=‘fistname’的所有元素

*: *,选择所有的元素

element: p,选择所有的p标签的元素

element,element: div,p,选择所有的div标签元素和所有p标签的元素

element element: div p,选择div标签元素内部的所有p标签元素

element>element: div>p,选择父元素为div标签元素的所有p标签元素

element+element: div+p,选择紧接在div元素之后的所有p元素

[attribute]: [target],选择带有target属性的所有元素

[attritube=value]:[target=_blank],选择target=”_blank”的所有元素

[attritube~=value]:[title~=flower],选择title属性包含单词‘flower’的所有元素

[attritube|=value]:[lang|=en],选择lang属性值以‘en’开头的所有元素(css2)(selenium用不到)

:link a:link,选择所有未被访问的链接

:visited a:visited,选择所有已被访问的链接

:active a:active,选择活动链接

:hover a:hover,选择鼠标指针位于其上的链接

:focus input:focus,选择获得焦点的input元素

:first-letter p:first-letter,选择每个p标签元素的首字母(获取p标签包含文本的首字母)

:first-line p:first-line,选择每个p标签元素的首行(获取p标签包含文本的首行)

:first-child p:first-child,选择属于父元素的第一个子元素的每个p标签元素

:before p:before,在每个p标签元素的内容之前插入内。(样式指定内容插入到p标签包括的文本之前)

:after p:after,在每个p标签元素的内容之后插入内容。(样式指定内容插入到p标签包括的文本之后)

:lang(language) p:lang(it),选择的lang属性值等于it的每个p标签元素

element1~element2 p~ul,选择前面有p标签元素的每一个ul标签元素(只要前面有p标签都可以,不管跨不跨标签,也不管是不是子标签)

[attribute^=value] a[src^=’https’],选择其src属性以’https’开头的每个a标签元素

[attribute$=value] a[src$=’.pdf’],选择其src属性以’.pdf’结尾的所有a标签元素

[attribute*=value] a[src*=’abc’],选择其src属性包含’abc’子串的每个a标签元素

:first-of-type p:first-of-type,选择父元素的首个p标签元素的每个p标签元素(所有p标签对应的父标签的第一个子p标签元素)

:last-of-type p:last-of-type,选择父元素的最后一个p标签元素的每个p标签元素(所有p标签对应的父标签的最后一个子p标签元素)

:only-of-type p:only-of-type,选择父元素里面唯一的p标签元素的每个p标签元素(所有p标签对应的父标签元素只有一个子p标签元素)

:only-child p:only-child,选择父元素里面只有一个标签的每个p标签元素(父标签里面只有一个p标签)

:nth-child(n) p:nth-child(2),选择属于父元素的第二个子元素的每个p标签元素(所有的p标签的父标签对应的第二个子标签元素)

:nth-last-child(n) p:nth-last-child(2),选择属于父元素的倒数第二个子元素的每个p标签元素(所有的p标签的父标签对应的倒数第二个子标签元素)

:nth-of-type(n) p:nth-of-type(2),选择属于其父标签元素的第二个p标签的每个p标签元素(所有p标签对应的父标签的第二个p标签元素,也就是这个和p标签平级)

:nth-last-of-type(n) p:nth-last-of-type(2),选择属于其父标签元素的倒数第二个p标签的每个p标签元素(所有p标签对应的父标签的倒数第二个p标签元素,也就是这个和p标签平级)

:last-child p:last-child,选择属于其父标签元素最后一个子元素是p标签的每个p标签元素(父标签的最后一个标签必须是p标签)

:root :root,选择文档的根元素

:empty p:empty,选择没有子元素的每个p标签元素(包括文本节点,无法理解)

:target #news:target,选择当前活动的#news元素(锚点定位)

:enabled input:enabled,选择每个启用input标签元素(input文本可编辑)

:disabled input:disabled,选择每个禁用input标签元素(input文本不可编辑)

:checked input:checked,选择每个被选中的input标签元素

:not(selector) :not(p),选择非p标签元素的每个元素

::selection ::selection,选择被用户选取的元素部分(选取文本会被样式控制)

selenium通过css定位常用的有:

通过class定位,一个标签有多个class中间的空格用点代替,通过父标签的父标签定位p,在通过p定位子标签input,通过标签+class定位

#element element: div p,选择div标签元素内部的所有p标签元素

#element>element: div>p,选择父元素为div标签元素的所有p标签元素

#element element: div p,选择div标签元素内部的所有p标签元素

#element>element: div>p,选择父元素为div标签元素的所有p标签元素

#element+element: div+p,选择紧接在div元素之后的所有p元素

#id: #firstname,选择id=‘fistname’的所有元素

#[attritube=value]:[target=_blank],选择target=”_blank”的所有元素

#[attribute]: [target],选择带有target属性的所有元素

#[attritube~=value]:[title~=flower],选择title属性包含单词‘flower’的所有元素

#:first-child p:first-child,选择属于父元素的第一个子元素的每个p标签元素

#[attribute^=value] a[src^=’https’],选择其src属性以’https’开头的每个a标签元素

#[attribute$=value] a[src$=’.pdf’],选择其src属性以’.pdf’结尾的所有a标签元素

#[attribute*=value] a[src*=’abc’],选择其src属性包含’abc’子串的每个a标签元素

#:first-of-type p:first-of-type,选择父元素的首个p标签元素的每个p标签元素(所有p标签对应的父标签的第一个子p标签元素)

#:last-of-type p:last-of-type,选择父元素的最后一个p标签元素的每个p标签元素(所有p标签对应的父标签的最后一个子p标签元素)

#:only-of-type p:only-of-type,选择父元素里面唯一的p标签元素的每个p标签元素(所有p标签对应的父标签元素只有一个子p标签元素)

#:nth-child(n) p:nth-child(2),选择属于父元素的第二个子元素的每个p标签元素(所有的p标签的父标签对应的第二个子标签元素)

#:only-child p:only-child,选择父元素里面只有一个标签的每个p标签元素(父标签里面只有一个p标签)

#:nth-last-child(n) p:nth-last-child(2),选择属于父元素的倒数第二个子元素的每个p标签元素(所有的p标签的父标签对应的倒数第二个子标签元素)

#:nth-of-type(n) p:nth-of-type(2),选择属于其父标签元素的第二个p标签的每个p标签元素(所有p标签对应的父标签的第二个p标签元素,也就是这个和p标签平级)

#鼠标放上悬浮选择菜单ActionChains(browser).move_to_element(article).perform()需要导入ActionChains包from selenium.webdriver.common.action_chains import ActionChains

#:nth-last-of-type(n) p:nth-last-of-type(2),选择属于其父标签元素的倒数第二个p标签的每个p标签元素(所有p标签对应的父标签的倒数第二个p标签元素,也就是这个和p标签平级)

#:last-child p:last-child,选择属于其父标签元素最后一个子元素是p标签的每个p标签元素(父标签的最后一个标签必须是p标签)

#coding=utf-8 from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains browser=webdriver.Chrome() browser.maximize_window() browser.get(地址) #通过class定位,一个标签有多个class中间的空格用点代替 browser.find_element_by_css_selector(.ant-input.inp.mt28).send_keys(ceshi) #通过父标签的父标签定位p,在通过p定位子标签input browser.find_element_by_css_selector(div.login-main>p:nth-child(2)>input).send_keys(123456) #通过标签+class定位 browser.find_element_by_css_selector(button.ant-btn.ant-btn-primary).click() time.sleep(1) #element element: div p,选择div标签元素内部的所有p标签元素 browser.find_element_by_css_selector(div.icon-pass-change-div i).click() time.sleep(1) #element>element: div>p,选择父元素为div标签元素的所有p标签元素 browser.find_element_by_css_selector(button.ant-modal-close>span).click() time.sleep(1) #element+element: div+p,选择紧接在div元素之后的所有p元素 browser.find_element_by_css_selector(div.user-name+div>i).click() time.sleep(1) #id: #firstname,选择id=‘fistname’的所有元素 browser.find_element_by_css_selector(#sourcePwd).send_keys(wafer123) #[attritube=value]:[target=_blank],选择target="_blank"的所有元素 browser.find_element_by_css_selector(#newPassword[type="password"]).send_keys(123456) #[attribute]: [target],选择带有target属性的所有元素 browser.find_element_by_css_selector(#confirmPassword[placeholder]).send_keys(123456) #[attritube~=value]:[title~=flower],选择title属性包含单词‘flower’的所有元素 browser.find_element_by_css_selector(button[class~=ant-btn]:nth-child(1)).click() #:first-child p:first-child,选择属于父元素的第一个子元素的每个p标签元素 time.sleep(1) browser.find_element_by_css_selector(.dept-chose-icon>i:first-child).click() time.sleep(1) #[attribute^=value] a[src^=https],选择其src属性以https开头的每个a标签元素 browser.find_element_by_css_selector(button[aria-label^="Close"]>span).click() time.sleep(1) #[attribute$=value] a[src$=.pdf],选择其src属性以.pdf结尾的所有a标签元素 browser.find_element_by_css_selector(i[class$="anticon anticon-plus-circle-o"]).click() time.sleep(1) #[attribute*=value] a[src*=abc],选择其src属性包含abc子串的每个a标签元素 browser.find_element_by_css_selector(.ant-modal-body>span[class*="ant-select"]).click() time.sleep(1) #:first-of-type p:first-of-type,选择父元素的首个p标签元素的每个p标签元素(所有p标签对应的父标签的第一个子p标签元素) browser.find_element_by_css_selector(.ant-select-tree>li:first-of-type).click() time.sleep(0.5) #:last-of-type p:last-of-type,选择父元素的最后一个p标签元素的每个p标签元素(所有p标签对应的父标签的最后一个子p标签元素) browser.find_element_by_css_selector(.ant-modal-footer>button:last-of-type).click() time.sleep(0.5) #:only-of-type p:only-of-type,选择父元素里面唯一的p标签元素的每个p标签元素(所有p标签对应的父标签元素只有一个子p标签元素) browser.find_element_by_css_selector(.dept-chose-icon>i:only-of-type).click() time.sleep(0.5) #:nth-child(n) p:nth-child(2),选择属于父元素的第二个子元素的每个p标签元素(所有的p标签的父标签对应的第二个子标签元素) browser.find_element_by_css_selector(.ant-modal-footer>button:nth-child(1)).click() time.sleep(0.5) #:only-child p:only-child,选择父元素里面只有一个标签的每个p标签元素(父标签里面只有一个p标签) browser.find_element_by_css_selector(.ant-menu-submenu-title>i:only-child).click() #:nth-last-child(n) p:nth-last-child(2),选择属于父元素的倒数第二个子元素的每个p标签元素(所有的p标签的父标签对应的倒数第二个子标签元素) browser.find_element_by_css_selector(#item_0$Menu>li:nth-last-child(2)).click()#id里面有$符号需要转义一下 time.sleep(0.5) #:nth-of-type(n) p:nth-of-type(2),选择属于其父标签元素的第二个p标签的每个p标签元素(所有p标签对应的父标签的第二个p标签元素,也就是这个和p标签平级) article=browser.find_element_by_css_selector(.ant-menu-submenu-title>i:nth-of-type(1)) #鼠标放上悬浮选择菜单ActionChains(browser).move_to_element(article).perform()需要导入ActionChains包 ActionChains(browser).move_to_element(article).perform() time.sleep(0.5) #:nth-last-of-type(n) p:nth-last-of-type(2),选择属于其父标签元素的倒数第二个p标签的每个p标签元素(所有p标签对应的父标签的倒数第二个p标签元素,也就是这个和p标签平级) browser.find_element_by_css_selector(#item_0$Menu>li:nth-last-of-type(2)).click() time.sleep(1) #:last-child p:last-child,选择属于其父标签元素最后一个子元素是p标签的每个p标签元素(父标签的最后一个标签必须是p标签) browser.find_element_by_css_selector(.user-info>div:last-child).click() time.sleep(0.5) browser.find_element_by_css_selector(.ant-confirm-btns>button:nth-child(2)).click() browser.close()

网站地图

Copyright © 2002-2022 香港德胜云网络 版权所有 | 备案号:蜀ICP备2023007363号-5

声明: 本站内容全部来自互联网,非盈利性网站仅供学习交流