无需选择即可自动执行Selenium中的下拉菜单

时间:2018-07-03 17:17:35

标签: html css python-3.x selenium

我正在尝试使用Selenium从下拉列表中选择一个元素。我已经可以选择下拉菜单,但是我不知道如何从下拉菜单中选择特定的元素,因为该网站不使用select,因此不能使用内置的select类。作为参考,这是下拉列表中元素之一的HTML。

<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; left: 75px; width: 188px; height: 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>

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您可以尝试以下方法:

 /* Timer event to move the text from right to left. */
 /* The name of the Label control is myLabel. */
 private void MoveLabelText_Tick(object sender, EventArgs e)
 {
     this.myLabel.Text = this.myLabel.Text.Substring(1, this.myLabel.Text.Length - 1) + this.myLabel.Text.Substring(0, 1);
 }

您也可以尝试以下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

因为我不确定//content[contains(., 'BlainSupply')]/parent::div 标签是否可点击