WebElement Object.text返回无法剥离的特定字符串

2024-06-07 15:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我将SeleniumWebDriver与Python3一起使用。 我使用以下命令提取了一个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,并将其保存到变量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”并执行上述任何操作,它都会工作。有人对此有解释和解决办法吗

已使用的剥离、替换、拆分和复制。复制


Tags: searchdatatitleliultempreplaceclass
1条回答
网友
1楼 · 发布于 2024-06-07 15:37:47

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

首先,拆分(“×”)

相关问题 更多 >

    热门问题