只有将鼠标悬停在鼠标上方时才会显示下拉菜单。
我在网上尝试了很多解决方案而没有运气:
他们中的大多数人都认为标签名称是"选项",而对我来说,HTML有点不同。 HTML在下面,顶级memu是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>
欢迎提出任何建议。
答案 0 :(得分:3)
看起来li
id="m1_m5"
是列表项之间的分隔符,您可能需要它之后的下一个li
元素,您可以使用xpath和{{3}获取它}:
# 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()
更新(悬停菜单,点击livingata的子菜单):
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()