python - 无法单击 selenium python 下拉菜单中的列表项

标签 python selenium xpath

我想单击导出下拉按钮下的 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
'值(value)':值(value)})['值(value)']
文件“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'] "}
( session 信息:chrome=58.0.3029.110)
(驱动程序信息:chromedriver=2.29.461591(62ebf098771772160f391d75e589dc567915b233),平台=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()

最佳答案

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

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

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

关于python - 无法单击 selenium python 下拉菜单中的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44411054/

相关文章:

Python - unicode 字符串元组,编码错误

Python:打印树的所有节点无意中存储数据

java - Selenium Select 对象的 selectByIndex 方法检查索引属性而不是计数。为什么?

css - 如何找到 div 元素的深度标签的 xpaths?

python - 按标题类型拆分 HTML div 后,如何提取我感兴趣的内容?

python - 在 Python 中使 OS 打开目录

Python。 Tornado 。非阻塞 xmlrpc 客户端

java - 如果它是两个组的成员,是否可以为 TestNG 设置一个条件来运行测试?

Python 2.7 Selenium 网站上没有这样的元素

java - 'UnexpectedTagNameException' 和 Element 应该是 "select",但通过 Selenium java 使用 "div"函数却出现 'Select' 错误