如何使用多个元素的选择?



我正在做一个报告自动化,但我需要选择几个过滤器,当我选择它时,它不会,代码如下:

b = chrome.find_element_by_xpath('//*[@id="Filters"]/div[1]/form/div[2]/div/div[10]/div/span/span[1]/span/ul')
c = Select(b)
c.select_by_visible_text('7474 - 0800 Berlanda')

首先我选择了所有的选项来看看它被选中的时候是什么样子的它看起来是这样的

<li class="select2-selection__choice" title=" 7474 - 0800 Berlanda  " data-select2-id="85"><span class="select2-selection__choice__remove" role="presentation">×</span> 7474 - 0800 Berlanda  </li>
<li class="select2-selection__choice" title=" 9800 - 0800 JOSY MIX " data-select2-id="86"><span class="select2-selection__choice__remove" role="presentation">×</span> 9800 - 0800 JOSY MIX </li>
<li class="select2-selection__choice" title=" 2222 - 0800 - Oi Vendas " data-select2-id="87"><span class="select2-selection__choice__remove" role="presentation">×</span> 2222 - 0800 - Oi Vendas </li>
<li class="select2-selection__choice" title=" 6060 - 0800 MM " data-select2-id="88"><span class="select2-selection__choice__remove" role="presentation">×</span> 6060 - 0800 MM </li>
<li class="select2-selection__choice" title=" 7070 - 0800 VUON CARD " data-select2-id="89"><span class="select2-selection__choice__remove" role="presentation">×</span> 7070 - 0800 VUON CARD </li>
I tried to copy the xpath of these options to see if that way he selected everything else didn't work, can you help me please?
**EDIT**
Here's the selection of the elements I'm trying to select:
<li class="select2-results__option" id="select2-ufab-result-kl9a-48" role="option" aria-selected="false" data-select2-id="select2-ufab-result-kl9a-48"> 7474 - 0800 Berlanda  </li>
<li class="select2-results__option" id="select2-ufab-result-9mym-54" role="option" aria-selected="false" data-select2-id="select2-ufab-result-9mym-54"> 9800 - 0800 JOSY MIX </li>
<li class="select2-results__option select2-results__option--highlighted" id="select2-ufab-result-bx84-62" role="option" aria-selected="false" data-select2-id="select2-ufab-result-bx84-62"> 2222 - 0800 - Oi Vendas </li>
<li class="select2-results__option" id="select2-ufab-result-1fdm-13" role="option" aria-selected="false" data-select2-id="select2-ufab-result-1fdm-13"> 6060 - 0800 MM </li>
<li class="select2-results__option" id="select2-ufab-result-zse8-199" role="option" aria-selected="false" data-select2-id="select2-ufab-result-zse8-199"> 7070 - 0800 VUON CARD </li>

我想你弄错了。

问题解释:

查看HTML

<li class="select2-selection__choice" title=" 7474 - 0800 Berlanda  " data-select2-id="85"><span class="select2-selection__choice__remove" role="presentation">×</span> 7474 - 0800 Berlanda  </li>

有一个li标签,它的类是select2-selection__choice,类的值是包含select2

你不能使用Select(b),因为Select(web element)是用于下拉菜单,它是在HTML中使用Select和option标签构建的。

解决方案
  1. 你应该直接点击下拉菜单打开选项

  2. 一旦选项可见,直接点击你想要的。

像这样:

time.sleep(5)
b = chrome.find_element_by_xpath('//*[@id="Filters"]/div[1]/form/div[2]/div/div[10]/div/span/span[1]/span/ul')
b.click()
# Now options should be visible
time.sleep(5)
option = 
chrome.find_element_by_xpath("//li[contains(@text,'Berlanda')]")
option.click()

最新更新