Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法单击ul标签下的li元素_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 无法单击ul标签下的li元素

Python 无法单击ul标签下的li元素,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我有以下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="

我有以下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>
我甚至尝试过使用类似于以下的东西:

    #now find Documents link and click
    recent = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Recent")))
    recent.click()

然后我得到一个超时错误。可能是我使用的组合错误或什么。

您可以尝试使用位于的元素的预期条件
可见性,\u,如下所示:

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()
更新1:请尝试以下操作:

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()

如果您仍然面临此问题,请告诉我。

请修复您的缩进!你是从chrome copy xpath中得到这个xpath的吗?我是用mozilla从firepath中得到这个xpath的。修复了缩进如果一切都失败,请尝试使用JavaScriptExecutor是的,它确实前进了一步,点击了下拉菜单,下拉菜单打开了,但它只点击了下拉菜单值“最近的”回溯(最后一次最近的调用):文件“C:\Python27\basicactionsselenium quikr.py”,第98行,在test_登录中等待。直到(元素的EC.visibility_位于((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(消息、屏幕、堆栈跟踪)TimeoutException:消息:如果您仍然面临此问题,请在我的回答中参考'UPDATE1',并让我知道。是的,它正在工作。非常感谢您的帮助。单击后下拉列表仍然打开,但我认为在添加更多步骤后,它将消失。”。你能帮助我理解:1-我做错了什么。2-您所做的以及如何获得代码中使用的xpath?据我所知,在第一段代码中,您等待ul加载/可见,在第二部分中,您等待li可见。我说得对吗。我以前的代码也类似。请告诉我为什么它不工作。我有一个类似的问题:
  • 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()