【问题标题】:Not able to click list item in a dropdown menu in selenium python无法单击 selenium python 下拉菜单中的列表项
【发布时间】:2017-11-08 17:06:23
【问题描述】:

我想单击导出下拉按钮下的 excel 按钮。但即使我能够访问菜单。当我尝试单击它下面的 excel 选项时,会显示一条消息,无法找到元素。

这是确切的错误消息: 回溯(最近一次通话最后): 文件“C:/Users/shishir sinha/PycharmProjects/australia/australia.py”,第 33 行,在 driver.find_element_by_xpath(".//[@id='ui-menu-0-1']").click() 文件“C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 309 行,在 find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) 文件“C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 787 行,在 find_element '价值':价值})['价值'] 文件“C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 252 行,在执行 self.error_handler.check_response(响应) 文件“C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py”,第 194 行,在 check_response 引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":".//[@id='ui-menu-0 -1']"} (会话信息:chrome=58.0.3029.110) (驱动信息:chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.14393 x86_64)

这是网站的链接: https://stats.oecd.org/Index.aspx?DataSetCode=STAN08BIS

代码如下:

 _author_ = 'shishir'
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



driver=webdriver.Chrome("C:\\Users\\shishir sinha\\AppData\\Local\\Programs\\Python\\Python36\\selenium\\webdriver\\chromedriver_win32\\chromedriver.exe")
driver.get("https://stats.oecd.org/Index.aspx?DataSetCode=STAN08BIS")



driver.find_element_by_xpath(".//*[@id='PDim_COU']").click()
driver.find_element_by_xpath(".//*[@id='PDim_COU']/option[1]").click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH, ".//*[@id='PDim_VAR']")))
driver.find_element_by_xpath(".//*[@id='PDim_VAR']").click()

action = webdriver.ActionChains(driver)


driver.find_element_by_xpath(".//*[@id='PDim_VAR']/option[3]").click()



WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, ".//*[@id='menubar-export']/a/span[1]/span[2]")))
driver.find_element_by_xpath(".//*[@id='menubar-export']/a/span[1]/span[2]").click()
driver.find_element_by_xpath(".//*[@id='menubar-export']/a/span[1]/span[2]").click()
action.move_to_element(driver.find_element_by_xpath(".//*[@id='menubar-export']/a/span[1]/span[2]"))
driver.find_element_by_xpath(".//*[@id='ui-menu-0-1']").click()

【问题讨论】:

  • 您可以考虑缩小到确切的行您是否面临错误?使用您尝试执行的手动步骤进行更新。谢谢

标签: python selenium xpath


【解决方案1】:

很可能实际的<select> 元素在页面上不可见,并且您在页面上看到的选择是伪选择元素,例如由<div><li> 元素组成。

这现在变得越来越普遍,因为用于设置原生选择样式的选项有限。因此,许多框架用更好看的组件“覆盖”它们,并通过 javascript 将逻辑委托给隐藏的底层选择。

解决方案是找到实际可见元素并单击它们而不是 <select> 或使用 driver.execute_script(...) 通过 javascript 驱动小部件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2020-05-26
    • 2020-04-20
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多