首页 > 解决方案 > 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×Testtype(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

标签: pythonselenium-webdriver

解决方案


“×XX\n×admin\n×Test”开头的“小x”不是“x”而是乘号。(Alt + 0215 = ×)。您可以从字符串中复制并粘贴乘号或使用下面的代码。

first.split('×')


推荐阅读