【问题标题】:Not able to click on li element under ul tag无法点击 ul 标签下的 li 元素
【发布时间】: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


【解决方案1】:

您可以尝试使用预期条件visibility_of_element_located,如下图:

driver.find_element_by_xpath("//div[@class='resultDiv']/div[@class='dropdown num-record']/button[@id='dLabel']")).click()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class='resultDiv']/div[@class='dropdown num-record']/ul[@aria-labelledby='dLabel']/li[text()='Recent']')))
driver.find_element_by_xpath("//div[@class='resultDiv']/div[@class='dropdown num-record']/ul[@aria-labelledby='dLabel']/li[text()='Recent']").click()

UPDATE1:尝试以下操作:

driver.find_element_by_xpath("//div[@class='resultDiv']/div[@class='dropdown num-record']/button[@id='dLabel']")).click()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class='resultDiv']/descendant::li[text()='Recent']')))
driver.find_element_by_xpath("//div[@class='resultDiv']/descendant::li[text()='Recent']").click()

如果您仍然遇到此问题,请告诉我。

【讨论】:

  • 是的,它确实提前了一步,它点击了下拉菜单并打开了下拉菜单,但它点击了下拉值“最近”
  • 回溯(最近一次调用最后):文件“C:\Python27\basicactionsselenium-quikr.py”,第 98 行,在 test_Login wait.until(EC.visibility_of_element_located((By.XPATH, "/ /div[@class='resultDiv']/div[@class='dropdown num-record']/ul[@aria-labelledby='dLabel']/li[text()='Recent']")))文件“C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py”,第 80 行,直到引发 TimeoutException(message, screen, stacktrace) TimeoutException: Message:
  • 如果您仍然遇到问题,请在我的回答中参考“UPDATE1”并告诉我。
  • 是的,它现在正在工作。非常感谢您的帮助。单击后下拉菜单仍然打开,但我认为添加更多步骤后它会消失。你能帮我理解:1-我做错了什么。 2-你做了什么以及你是如何得到你在代码中使用的 xpath 据我了解的是,在你的第一个代码中你正在等待 ul 被加载/可见,而在第二部分你正在等待 li变得可见。我是对的。我之前的代码也类似。请告诉我为什么它不起作用。
  • 我有一个类似的问题:
猜你喜欢
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 2019-01-24
相关资源
最近更新 更多