无法在selenium python

时间:2017-06-07 11:11:40

标签: python selenium xpath

我想单击导出下拉按钮下的excel按钮。但是我甚至可以访问菜单。当我尝试单击它下面的excel选项时,显示的消息无法找到元素。

以下是确切的错误消息: Traceback(最近一次调用最后一次):   文件“C:/ Users / shishir sinha / PycharmProjects / australia / australia.py”,第33行,in     driver.find_element_by_xpath( “.// [@ ID = 'UI菜单-0-1']”)。单击()   在find_element_by_xpath中输入文件“C:\ Users \ shishir sinha \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,第309行     return self.find_element(by = By.XPATH,value = xpath)   在find_element中输入文件“C:\ Users \ shishir sinha \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,第787行     '价值':价值})['价值']   文件“C:\ Users \ shishir sinha \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,第252行,执行     self.error_handler.check_response(响应)   在check_response中输入文件“C:\ Users \ shishir sinha \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py”,第194行     提出exception_class(消息,屏幕,堆栈跟踪) 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()

1 个答案:

答案 0 :(得分:0)

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

鉴于样式化本地选择的选项有限,这种情况现在变得越来越普遍。结果,许多框架将它们与外观更好的组件“覆盖”在一起,并通过javascript将逻辑委托给隐藏的基础选择。

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

相关问题