python - Selenium无法定位到指定元素:Element is not currently visible ?
ringa_lee
ringa_lee 2017-04-18 09:14:20
[Python讨论组]

最近学习Selenium,想做一个58后台自动发帖程序, 在修改工作地址时无法定位到指定元素位置,请高手指点!

网址如下:https://vip.58.com/fun/postpo...

默认显示地址为“北京-朝阳-东大桥”,希望修改为其他地址,

代码如下:

from selenium import webdriver
driver = webdriver.Firefox()
url_vip = 'https://vip.58.com/'

#打开网址并跳转到发帖页面
driver.get(url_vip)
driver.find_element_by_link_text(u"会员首页").click()
driver.find_element_by_id('15').click()
driver.switch_to.frame('ContainerFrame')
driver.find_element_by_css_selector("a.qz_btn").click()
driver.switch_to.frame('ContainerFrame')
driver.switch_to.frame('ContainerFrame')

#修改发布地址(遇到问题)
driver.find_element_by_link_text(u"点击发布到其他地区").click() #成功
driver.find_element_by_id('seleCompCirc').click()    #失败

报错信息如下:

Traceback (most recent call last):
  File "/home/malone/malone/58/58fatiezi.py", line 449, in 
    main()
  File "/home/malone/malone/58/58fatiezi.py", line 446, in main
    inputinfo()
  File "/home/malone/malone/58/58fatiezi.py", line 426, in inputinfo
    driver.find_element_by_id('seleCompCirc').click()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 73, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 456, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///tmp/tmpoUmtve/webdriver-py-profilecopy/extensions/fxdriver@googlecode.com/components/command-processor.js:10092)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpoUmtve/webdriver-py-profilecopy/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpoUmtve/webdriver-py-profilecopy/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpoUmtve/webdriver-py-profilecopy/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpoUmtve/webdriver-py-profilecopy/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

driver.find_element_by_link_text(u"点击发布到其他地区").click()执行前,p为隐藏状态:

命令执行后,通过 driver.find_element_by_id('seleCompCirc').click() 定位并点击东大桥时出现报错:

网页源代码如下:

xxxxxxxx ( 北京-朝阳-东大桥 ) 修改 新增

该职位将显示在北京-朝阳-东大桥下。 点击发布到其他地区

北京

朝阳

东大桥

还想过使用JS的方法进行处理,但也没有成功:


#修改发布地址(遇到问题)
driver.find_element_by_link_text(u"点击发布到其他地区").click()
driver.find_element_by_id('seleCompCirc').click()
js_quyu_num = "document.getElementById('seleCompCirc').k='5786';"
driver.execute_script(js_quyu_num)
js_quyu_name = "document.getElementById('seleCompCirc').innerText=u'四惠';" 
driver.execute_script(js_quyu_name)

命令执成功但是并没有修改

东大桥

的值,本人不懂JS,Orz。

ringa_lee
ringa_lee

ringa_lee

全部回复(1)
伊谢尔伦

问题已经解决:

通过driver.find_element_by_xpath(".//*[@id='tr_city']/td/p/p/p[3]").click()定位成功

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号