Работа с выпадающим списком в Selenium - PullRequest
0 голосов
/ 17 июня 2020

Я хочу автоматизировать нажатие на случайную опцию в раскрывающихся списках Google Form , например, в следующем:

enter image description here

Поскольку структура выглядит следующим образом:

<div jscontroller="liFoG" jsaction="rcuQ6b:vZc4S;O22p3e:zjh6rb;b2trFe:eVidQc;JIbuQc:d3sQLd;" class="freebirdFormviewerComponentsQuestionSelectRoot"><div role="listbox" aria-expanded="true" class="quantumWizMenuPaperselectEl appsMaterialWizMenuPaperselectSelect freebirdFormviewerComponentsQuestionSelectSelect freebirdThemedSelectDarkerDisabled noMaxWidth isOpen" jscontroller="YwHGTd" jsaction="click:cOuCgd(LgbsSe); keydown:I481le; keypress:Kr2w4b; mousedown:UX7yZ(LgbsSe),npT2md(preventDefault=true); mouseup:lbsD7e(LgbsSe); mouseleave:JywGue; touchstart:p6p2H(LgbsSe); touchmove:FwuNnf; touchend:yfqBxc(LgbsSe|preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd(LgbsSe); focus:AHmuwe; blur:O22p3e;b5SvAb:TvD9Pc;" jsshadow="" jsname="W85ice" aria-describedby="i2 i3" aria-labelledby="i1"><div jsname="LgbsSe" role="presentation" aria-hidden="true"><div class="quantumWizMenuPaperselectOptionList" jsname="d9BH4c" role="presentation" aria-hidden="true"><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder" jsname="wQNmvb" jsaction="" data-value="" aria-selected="true" tabindex="0"><div class="quantumWizMenuPaperselectRipple exportInk quantumWizMenuPaperselectSelectedRipple" jsname="ksKsZd" style="top: 23.7px; left: 123px; width: 176px; height: 176px;"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">Sélectionner</span></div><div class="appsMaterialWizMenuPaperselectOptionSeparator" role="presentation"></div><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="Yes" aria-selected="false" tabindex="-1"><div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">Yes</span></div><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="No" aria-selected="false" tabindex="-1"><div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">No</span></div><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="I'm not sure" aria-selected="false" tabindex="-1"><div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">I'm not sure</span></div></div><div class="quantumWizMenuPaperselectDropDown exportDropDown" role="presentation"></div></div><div class="exportSelectPopup quantumWizMenuPaperselectPopup appsMaterialWizMenuPaperselectPopup" soy-server-key="5:pZtlf" jsaction="click:dPTK6c(wQNmvb); mousedown:uYU8jb(wQNmvb); mouseup:LVEdXd(wQNmvb); mouseover:nfXz1e(wQNmvb); touchstart:Rh2fre(wQNmvb); touchmove:hvFWtf(wQNmvb); touchend:MkF9r(wQNmvb|preventMouseEvents=true)" role="presentation" jsname="V68bde" style="min-width: 178px; top: 281.5px;"><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder" jsname="wQNmvb" jsaction="" data-value="" aria-selected="true" role="option" tabindex="0"><div class="quantumWizMenuPaperselectRipple exportInk quantumWizMenuPaperselectSelectedRipple" jsname="ksKsZd" style="top: 23.7px; left: 123px; width: 176px; height: 176px;"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">Sélectionner</span></div><div class="appsMaterialWizMenuPaperselectOptionSeparator" role="presentation"></div><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="Yes" aria-selected="false" role="option" tabindex="-1"><div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">Yes</span></div><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="No" aria-selected="false" role="option" tabindex="-1"><div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">No</span></div><div class="quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="I'm not sure" aria-selected="false" role="option" tabindex="-1"><div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div><span jsslot="" class="quantumWizMenuPaperselectContent exportContent">I'm not sure</span></div></div></div></div>

Мне нужно сначала щелкнуть по списку, а затем по заданной опции.

Я попытался разобраться с этим с помощью этой группы code:

                 lists = container.find_element_by_class_name("quantumWizMenuPaperselectOption.appsMaterialWizMenuPaperselectOption.freebirdThemedSelectOptionDarkerDisabled.exportOption.isSelected.isPlaceholder")
                 if lists !=[]:
                     for list in lists:
                         option = container.find_element_by_class_name("quantumWizMenuPaperselectOption.appsMaterialWizMenuPaperselectOption.freebirdThemedSelectOptionDarkerDisabled.exportOption.isSelected")
                         if option:
                             option.click()

Но у меня проблема с ним ...

1 Ответ

0 голосов
/ 17 июня 2020

Используйте код ниже:

driver.find_element_by_class_name('quantumWizMenuPaperselectOptionList').click()
options = driver.find_elements_by_css_selector('.exportSelectPopup div.quantumWizMenuPaperselectOption')
random_index = randint(1, len(options))
options[random_index].click()

Пояснение:

  1. Найдите раскрывающийся элемент и нажмите
  2. Определите все раскрывающиеся значения элемент и сохранить в списке
  3. Сгенерировать случайное число между начальным индексом и максимальным размером списка (допустим, размер списка равен 4, а начальный индекс равен 1, поэтому он будет генерировать случайное число от 1 до 4)
  4. Нажмите на индекс случайных чисел в списке опций, который случайным образом выберет раскрывающееся значение
...