蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

元素定位:By、唯一性和上下文:独立本地浏览器实验

PYTHONWindows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话selenium-element-positioning.py
selenium-element-positioning.py11 行
from selenium_lab import *

@case('selenium-element-positioning')
def locating(l,d,b,s):
 expected=d.find_element(By.ID,'query');locators=[(By.NAME,'q'),(By.CLASS_NAME,'search-field'),(By.CSS_SELECTOR,'input.search-field.stable-field'),(By.XPATH,"//input[@data-testid='query']")]
 for how,value in locators:l.eq('same-element-'+how,d.find_element(how,value).id,expected.id)
 l.check('tag-many',len(d.find_elements(By.TAG_NAME,'input'))>=8);l.eq('link-text',d.find_element(By.LINK_TEXT,'查看学习计划').get_attribute('id'),'learn-link');l.eq('partial-link',d.find_element(By.PARTIAL_LINK_TEXT,'学习计划').get_attribute('id'),'learn-link');l.eq('empty-list',d.find_elements(By.ID,'not-present'),[]);l.err('missing-single',NoSuchElementException,lambda:d.find_element(By.ID,'not-present'));l.eq('scoped-relative-xpath',d.find_element(By.ID,'search-form').find_element(By.XPATH,'.//input').id,expected.id);l.eq('JS-returns-WebElement',d.execute_script("return document.querySelector('#query')").id,expected.id);relative=d.find_element(locate_with(By.TAG_NAME,'input').to_left_of(d.find_element(By.ID,'search-submit')));l.eq('geometry-relative-input',relative.id,expected.id);l.pending_case('ShadowRoot-and-compound-class','Open/closed shadow roots and all invalid compound CLASS_NAME cases not executed')

if __name__ == "__main__":
    run('selenium-element-positioning')
本例需在 Windows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话

入口文件:projects/selenium-lab/selenium-element-positioning.py

预期结果与验证条件观察提示
  • 本章固定输入断言通过,错误/等待/业务结果和退出分别验证
  • 截图对应本站当前测试页;未执行平台或能力不因本轮通过自动升级

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Selenium

课程关联工程包(登记关联15章) · 含当前章节或实例 · mbskycom202611a531.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习