【发布时间】:2017-05-22 18:52:30
【问题描述】:
我有以下 HTML 代码:
<div class="resultDiv">
<span class="">Sort by : </span>
<div class="dropdown num-record">
<button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="jsSortTypeText">Relevant</span>
<i class="icon-arrows_down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dLabel" style="display: none;">
<li class="jsSortType" data-value="Prem" data-label="VIP first">VIP first</li>
<li class="jsSortType" data-value="Rec" data-label="Recent">Recent</li>
<li class="jsSortType jsDefaultSort selected" data-value="Rel" data-label="Relevant">Relevant</li>
</ul>
</div>
</div>
我正在尝试单击元素“dLabel”或“ul”,然后单击“最近”选项。
我尝试使用动作链、xpath、css 选择器,但没有任何效果。当使用动作链并且测试运行时没有任何错误但点击没有发生。
有人可以指导我做错了什么。下面是我的代码,它运行时没有任何错误,但没有打开下拉菜单并选择“最近”选项。
menu1 = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(".//*[@id='dLabel']"))
clickon = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath("html/body/div[2]/div[2]/div[6]/div/div[3]/div[3]/div[1]/div/ul/li[2]"))
action = ActionChains(driver)
action.move_to_element(menu1).perform()
action.move_to_element(clickon)
action.click(clickon)
action.perform()
我什至尝试过使用类似下面的东西:
#now find Documents link and click
recent = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Recent")))
recent.click()
然后我得到一个超时错误。也许我使用了错误的组合或什么的。
【问题讨论】:
-
请修正你的缩进!
-
你是从 chrome copy xpath 得到这个 xpath 的吗?
-
我使用 mozilla 从 firepath 获得了 xpath。
-
修复缩进
-
如果一切都失败了尝试使用 JavascriptExecutor
标签: python selenium selenium-webdriver