我正在使用 Selenium webdriver 和 Python3。我使用以下命令提取了一个网络元素:
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
我会得到×XXn×adminn×Test
,type(ul.text)
返回class str
现在,希望摆脱开头的小"x",所以我拆分并获取xXX
的第一个子字符串并将其保存到变量 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"并执行上述任何操作,它都可以工作。有人对此有解释和解决方案吗?
使用过的条带、替换、拆分和复制.copy
"×XX×admin×Test"开头的"小x"不是"x",而是乘法符号。(Alt + 0215 = ×(。您可以从字符串中复制并粘贴乘号或使用我在下面的代码。
first.split('×'(