lok*_*oki 10 python selenium automation webdriver selenium-webdriver
我正在尝试自动化管理任务,到目前为止,我已经使selenium点击元素以显示下拉菜单.

当点击其中一个菜单元素时,我得到一个错误,说明必须显示该元素.
码:
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get(url)
doc = driver.find_element_by_css_selector('td.ms-vb-title > table')
try:
doc.click()
time.sleep(4)
menu = driver.find_element_by_xpath('//menu/span[5]')
time.sleep(4)
print dir(menu)
menu.click()
except:
traceback.print_exc()
driver.quit()
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "aprobar_docs.py", line 22, in main
menu.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 52, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 205, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 156, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 147, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: u'Element must be displayed to click'
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,代码等待很多东西才能加载元素.我也尝试将元素的is_displayed属性设置为True但不能正常工作.
注意:未显示的元素是xpath搜索中的元素,它存在,因为我用dir(菜单)记录了它
编辑:
该menu变量不是菜单本身它是菜单的元素跨度之一,doc是Perfil越来越点击显示下拉html元素.
编辑2:
检查chrome工具上的DOM,当你点击一个doc新的菜单在树上创建时,我不知道是因为ajax调用还是vanilla js,我认为它的创建方式并不重要.我无法从页面中检索它并从中创建一个python对象,它至少不会显示在代码上.
最终编辑:
我最终执行了一些JavaScript来使它工作.显然当Selenium找到菜单项时,触发菜单下拉菜单的第一个元素会失去焦点,并且如果您没有选择菜单项并等待一段时间仍然显示菜单下拉菜单,它会再次使菜单不可见,如果您尝试从菜单中选择一个元素菜单消失.
为什么不选择这样的选项
el = driver.find_element_by_id('id_of_select')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'The Options I Am Looking For':
option.click() # select() in earlier versions of webdriver
Run Code Online (Sandbox Code Playgroud)
如果您的点击未触发ajax调用以填充列表,则实际上不需要执行单击.
| 归档时间: |
|
| 查看次数: |
13243 次 |
| 最近记录: |