无需选择即可自动执行Selenium中的下拉菜单 [英] Automate Dropdown Menu in Selenium Without Select
问题描述
我正在尝试使用Selenium从下拉列表中选择一个元素.我已经可以选择下拉菜单,但是我不知道如何从下拉菜单中选择特定的元素,因为该网站不使用select,因此无法使用内置的select类.作为参考,这是下拉列表中元素之一的HTML.
I'm trying to select an element from a dropdown using Selenium. I'm already able to select the dropdown, but I don't know how to select the specific elements from the dropdown, since the website doesn't use select and thus I can't use the built-in select class. For reference, this is the HTML for one of the elements in the dropdown.
< div class ="Di ljb1sb hIqB1e LMgvRb" jsname ="wQNmvb" jsaction =" data-value ="118237948356370773614" aria-selected ="true" role ="option" tabindex ="0">< div class ="qm he" jsname ="ksKsZd" style ="top:23px;左侧:75px;宽度:188px;高度:188px;"></div>< content class ="u5 jh"> BlainSupply</content></div>
这是整个下拉列表的
<div role="listbox" aria-expanded="false" class="Ej BtzVgc" 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="" data-max-rows="10"><div jsname="LgbsSe" role="presentation">
<div class="EI" jsname="d9BH4c" role="presentation">
<div class="Di ljb1sb hIqB1e LMgvRb bf" jsname="wQNmvb" jsaction="" data-value="118237948356370773614" aria-selected="true" tabindex="0" role="option">
<div class="qm he" jsname="ksKsZd" style="top: 27px; left: 92px; width: 197px; height: 197px;"></div>
<content class="u5 jh">BlainSupply</content>
</div>
<div class="Di ljb1sb hIqB1e LMgvRb" jsname="wQNmvb" jsaction="" data-value="118324169618367399437" aria-selected="false" tabindex="-1" role="option">
<div class="qm he" jsname="ksKsZd"></div>
<content class="u5 jh">GPlusPages02</content>
</div>
<div class="Di ljb1sb hIqB1e LMgvRb" jsname="wQNmvb" jsaction="" data-value="101010111938653639529" aria-selected="false" tabindex="-1" role="option">
<div class="qm he" jsname="ksKsZd"></div>
<content class="u5 jh">GSitemap2</content>
</div>
</div>
<div class="v5 VtTjbf eU809d" role="presentation"></div></div>
任何帮助将不胜感激!
推荐答案
您可以尝试以下操作:
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//content[contains(., 'BlainSupply')]")))
element.click()
作为替代方案,您可以尝试以下 xPath
:
as alternative you can try this xPath
:
//content[contains(., 'BlainSupply')]/parent::div
因为我不确定 content
标签是否可点击
because I'm not sure that content
tag is clickable
这篇关于无需选择即可自动执行Selenium中的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!