webdriver 定位方式

本文介绍了如何使用XPath通过多个属性定位元素的方法,包括直接属性匹配及使用contains函数进行模糊匹配的技术细节。


1、有时为了定位更可靠,所以需要使用xpath通过多个属性进行定位元素

//  从匹配选择的当前节点选择文档中的节点,而不考虑它们位置


driver.findElement(By.xpath("//input[@name='pass' and @class=’ textfild’]")).clear();



2、关于xpath 查找元素还可以通过contains(一个字符串查找函数)来实现,语法是

//input[contains(@id,vakue) and contains(@id,value)],此中的id和value就是定位input元素的键值对


### 如何使用 WebDriver 定位进度条末端 WebDriver 是一种用于自动化浏览器操作的强大工具,可以用来模拟用户的交互行为。对于定位页面上的特定元素(如进度条),通常可以通过其属性或状态来实现。 #### 使用 XPath 或 CSS Selector 定位进度条 在 HTML 中,进度条通常是通过 `<progress>` 标签或者自定义的 `div` 来表示。如果要找到进度条的末端位置,则需要先获取该元素的最大值以及当前值。以下是两种常见的方法: 1. **XPath 表达式** 如果知道进度条的具体结构,可以直接编写一个精确匹配的 XPath 查询语句。 ```python from selenium import webdriver driver = webdriver.Chrome() progress_bar = driver.find_element_by_xpath("//progress[@value]") # 找到带有 value 属性的 progress 元素[^3] max_value = int(progress_bar.get_attribute('max')) # 获取最大值 current_value = int(progress_bar.get_attribute('value')) # 当前完成度 ``` 2. **CSS Selectors** 对于更复杂的 DOM 结构,可能需要用 CSS selector 进行筛选。 ```python progress_bar_css = driver.find_element_by_css_selector("progress[value]") # 查找具有 'value' 的 progress 元素[^4] max_val = int(progress_bar_css.get_attribute('max')) curr_val = int(progress_bar_css.get_attribute('value')) ``` #### 判断进度条是否到达终点 一旦获得了进度条的最大值 (`max`) 和当前值 (`value`) 后,就可以比较两者的关系以判断进度条是否已经结束加载。 ```python if current_value >= max_value: print(f"Progress bar has reached the end with {current_value}/{max_value}.") # 输出提示信息表明已完成[^5] else: print(f"Still loading... Current status is {current_value}/{max_value}") ``` #### 自定义进度条处理方式 当遇到非标准标签构建而成的进度指示器时,比如某些网站采用动态更新宽度百分比的方式来展示进展过程,这时就需要额外考虑样式类名或者其他数据绑定机制来进行解析。 假设有一个基于 div 实现的假进度条如下所示: ```html <div class="custom-progress-bar"> <span style="width:80%;">Loaded</span> </div> ``` 那么可以用下面这段脚本来读取实际填充比例并推断最终目标点在哪里。 ```python custom_progress = driver.find_element_by_class_name("custom-progress-bar span") style_attr = custom_progress.get_attribute('style') percentage_loaded = float(style_attr.split(':')[1].strip('%;')) / 100 * 100 # 提取消数部分转换成数值形式[^6] print(f"The custom progress bar shows that it's at %{percentage_loaded:.2f} complete.") ``` 以上就是关于如何利用 Selenium Webdriver 配合 Python 编程语言去检测网页中的进度条何时达到尽头的一些基本技巧和实例代码片段。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值