python - WebElement Object.text 返回无法剥离的特定字符串
问题描述
我在 Python3 中使用 selenium webdriver。我使用以下命令提取了一个 webelement:
ul = driver.find_element(By.CSS_SELECTOR, '.select2-selection__rendered')
这是我正在处理的 HTML 剪辑:
<li class="select2-selection__choice" title="XX" data-select2-id="10"><span class="select2-selection__choice__remove" role="presentation">×</span>XX</li><li class="select2-selection__choice" title="admin" data-select2-id="11"><span class="select2-selection__choice__remove" role="presentation">×</span>admin</li><li class="select2-selection__choice" title="Test" data-select2-id="12"><span class="select2-selection__choice__remove" role="presentation">×</span>Test</li><li class="select2-search select2-search--inline"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="" style="width: 0.75em;"></li>
现在,如果我这样做,ul.text
我得到×XX\n×admin\n×Test
并type(ul.text)
返回class str
现在,希望摆脱开头的小“x”,所以我拆分并获取第一个子字符串xXX
并将其保存到 variable first
。我尝试了以下方法,但它不起作用:
first.strip('x')
first.replace('x', ' ') or first.replace('x', '')
first.split('x')
temp = copy.copy(first)
temp.split('x')
temp.replace('x', ' ') or temp.replace('x', '')
temp.strip('x')
但是,如果我手动编写“xXX”并执行上述任何操作,它就会起作用。有没有人对此有解释和解决方案?
使用strip、replace、split和copy.copy
解决方案
“×XX\n×admin\n×Test”开头的“小x”不是“x”而是乘号。(Alt + 0215 = ×)。您可以从字符串中复制并粘贴乘号或使用下面的代码。
first.split('×')
推荐阅读
- swiftui - 如何在 SwiftUI 下使用异步调用闭包来初始化参数
- c# - 使用 C# 在 VSTO Outlook 中的 mailitem.Body 的开头插入文本
- java - Android mpchart - 条形图添加网格线和交错颜色
- android-studio - Intellij (Android Studio) 是否有任何插件,其中实体代码分别格式化为属性和获取器和设置器?
- php - 如何将 laravel 6 升级到 7
- node.js - 使用 nodeJs 和 GraphQL 上传文档
- listview - 跨页面导航保持列表视图的索引
- android - 如何在 ACTIVITY 内部的 json 中调用适配器
- microsoft-edge-extension - 手动安装扩展
- php - 产品库存数量 - 购物车中的产品数量