自定义操作如何封装脚本
<div style="background-color:#1e1f22;color:#bcbec4"><pre style="font-family:'JetBrains Mono',monospace;font-size:9.8pt;"><span style="color:#7a7e85;"># </span><span style="color:#7a7e85;font-family:'宋体',monospace;">使用</span><span style="color:#7a7e85;">Selenium</span><span style="color:#7a7e85;font-family:'宋体',monospace;">的定位方法找到</span><span style="color:#7a7e85;">ul</span><span style="color:#7a7e85;font-family:'宋体',monospace;">元素<br></span>ul_element = driver.find_element_by_css_selector(<span style="color:#6aab73;">"body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul"</span>)<br><br><span style="color:#7a7e85;"># </span><span style="color:#7a7e85;font-family:'宋体',monospace;">使用</span><span style="color:#7a7e85;"> XPath </span><span style="color:#7a7e85;font-family:'宋体',monospace;">定位所有</span><span style="color:#7a7e85;"> li </span><span style="color:#7a7e85;font-family:'宋体',monospace;">元素<br></span>li_elements = ul_element.find_elements(By.XPATH, <span style="color:#6aab73;">".//li"</span>)<br><br>target_span_text = <span style="color:#6aab73;">"</span><span style="color:#6aab73;font-family:'宋体',monospace;">测试</span><span style="color:#6aab73;">222"<br></span><span style="color:#6aab73;"><br></span><span style="color:#7a7e85;"># </span><span style="color:#7a7e85;font-family:'宋体',monospace;">遍历</span><span style="color:#7a7e85;"> li </span><span style="color:#7a7e85;font-family:'宋体',monospace;">元素并进一步定位</span><span style="color:#7a7e85;"> div </span><span style="color:#7a7e85;font-family:'宋体',monospace;">和</span><span style="color:#7a7e85;"> span </span><span style="color:#7a7e85;font-family:'宋体',monospace;">元素来获取文本内容<br></span><span style="color:#cf8e6d;">for </span>li_element <span style="color:#cf8e6d;">in </span>li_elements:<br> <span style="color:#cf8e6d;">try</span>:<br> div_element = li_element.find_element(By.XPATH, <span style="color:#6aab73;">".//div"</span>)<br> span_elements = div_element.find_elements(By.XPATH, <span style="color:#6aab73;">".//span"</span>)<br><br> <span style="color:#cf8e6d;">for </span>span_element <span style="color:#cf8e6d;">in </span>span_elements:<br> <span style="color:#cf8e6d;">if </span>span_element.text == target_span_text:<br> <span style="color:#7a7e85;"># </span><span style="color:#7a7e85;font-family:'宋体',monospace;">创建</span><span style="color:#7a7e85;"> ActionChains </span><span style="color:#7a7e85;font-family:'宋体',monospace;">对象<br></span><span style="color:#7a7e85;font-family:'宋体',monospace;"> </span>actions = ActionChains(driver)<br> <span style="color:#7a7e85;"># </span><span style="color:#7a7e85;font-family:'宋体',monospace;">移动到要点击的元素上,然后执行点击操作<br></span><span style="color:#7a7e85;font-family:'宋体',monospace;"> </span>actions.move_to_element(span_element).click().perform()<br> <span style="color:#cf8e6d;">break<br></span><span style="color:#cf8e6d;"> except </span>StaleElementReferenceException:<br> <span style="color:#7a7e85;"># </span><span style="color:#7a7e85;font-family:'宋体',monospace;">处理元素已经过期的情况,可以在这里添加相应的处理逻辑<br></span><span style="color:#7a7e85;font-family:'宋体',monospace;"> </span><span style="color:#cf8e6d;">pass
</span><p> 这个是我要执行的脚本怎么在操作管理中封装来使用</p>
<pre style="font-size: 9.8pt; font-family: "JetBrains Mono", monospace;"><span style="color: rgb(207, 142, 109);">这个是我要执行的脚本怎么在</span></pre>
<span style="color:#cf8e6d;">
<br></span></pre></div>
收藏(0)
分享
相关标签:
注意:本文归作者所有,未经作者允许,不得转载