用Selenium处理下拉菜单

问题描述 投票:0回答:1

我想自动地在 谷歌表格 下拉列表,如下图所示。

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>

我需要先点击列表,然后再点击某个选项。

我试图用这段代码来处理它。

                 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()

但我有一个问题... ...

python python-3.x forms selenium dropdown
1个回答
0
投票

使用下面的代码。

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. 点击选项列表中的随机数指数,将随机选择一个下拉值。
© www.soinside.com 2019 - 2024. All rights reserved.