编程语言
首页 > 编程语言> > 如何使用Selenium和Chrome在Python中单击下拉菜单

如何使用Selenium和Chrome在Python中单击下拉菜单

作者:互联网

仅将鼠标悬停在下拉菜单上.

我没有运气就尝试了很多在线解决方案:

> https://qaquestions.wordpress.com/2012/03/05/selenium-web-driver-some-tricks-using-python/
> Selenium can’t find option form element by xpath
> Selecting a value from a drop-down option using selenium python

他们中的大多数人认为标签名称是“ option”,而对我来说HTML有点不同.
下面是HTML,顶层元素是m1.我想单击下拉菜单m1_m7.

<li class="item first"></li>
<li class="item" style="z-index: 0;">
    <a id="m1" class="link " href="www.example.com"></a>
    <div class="slide" style="z-index: 11; display: none; overflow: hidden; height: 184px; width: 151px; top: 26px; left: 0px;">
    <ul class="vertical group level1" style="z-index: 11; display: block; top: -184px;">
    <li class="item first" style="z-index: 0;"></li>
    <li id="m1_m1" class="item separator " style="width: 151px;"></li>
    <li class="item" style="z-index: 0;"></li>
    <li id="m1_m3" class="item separator " style="width: 151px;"></li>
    <li class="item" style="z-index: 0;"></li>
    <li id="m1_m5" class="item separator " style="width: 151px;"></li>
    <li class="item" style="z-index: 0;"></li>
    <li id="m1_m7" class="item separator " style="width: 151px;"></li>
        <li style="z-index: 0;" class="item"><a style="width: 151px;" href="www.example.com/sub8" id="m1_m8" class="link "><span class="text expandLeft">Benefits</span></a><div style="z-index: 42; display: none; overflow: hidden; height: 105px; width: 206px; top: 0px; left: 151px;" class="slide">
                    <ul style="z-index: 42; display: block; left: -206px;" class="vertical group level2">
                        <li class="item first"><a style="width: 206px;" href="www.example.com/sub8_0" id="m1_m8_m0" class="link "><span class="text">Patient Safety - Joint Commission</span></a></li><li style="width: 206px;" id="m1_m8_m1" class="item separator "><span class="text"></span></li><li class="item"><a style="width: 206px;" href="www.example.com/sub8_1" id="dnn_INGENIMENU1__ctl0_m1_m8_m2" class="link "><span class="text">Perioperative Efficiency</span></a></li><li style="width: 206px;" id="dnn_INGENIMENU1__ctl0_m1_m8_m3" class="item separator "><span class="text"></span></li><li class="item"><a style="width: 206px;" href="www.example.com/sub8_3" id="m1_m8_m4" class="link "><span class="text">SCIP Compliance</span></a></li><li style="width: 206px;" id="m1_m8_m5" class="item separator "><span class="text"></span></li><li class="item last"><a style="width: 206px;" href="www.example.com/sub8_5/" id="m1_m8_m6" class="link "><span class="text">Time Out Electronic Checklists</span></a></li>
                    </ul>
                </div></li>
            <li id="m1_m9" class="item separator " style="width: 151px;"></li>
    <li class="item" style="z-index: 0;"></li>
    <li id="m1_m11" class="item separator " style="width: 151px;"></li>
    <li class="item last"></li>

欢迎任何建议.

解决方法:

看起来id为“ m1_m5”的li是列表项之间的分隔符,您可能需要后面的下一个li元素,可以使用xpath和following-sibling来获取它:

# open up the dropdown
dropdown = driver.find_element_by_css_selector("ul.level1")
dropdown.click()

# select element
item = dropdown.find_element_by_xpath("//li[@id='m1_m5']/following-sibling::li")
item.click()

更新(将鼠标悬停在菜单上,单击实时数据上的子菜单):

from selenium import webdriver
from selenium.webdriver import ActionChains

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("http://www.livedata.com/")

wait = WebDriverWait(driver, 10)

healthcare = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='item' and a/span='Healthcare']")))
ActionChains(driver).move_to_element(healthcare).perform()

benefits = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='item']/a[span='Benefits']")))
ActionChains(driver).move_to_element(benefits).click().perform()

标签:selenium,selenium-webdriver,html,python
来源: https://codeday.me/bug/20191120/2044514.html