Selenium无法定位到指定元素:Element is not currently visible ?

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

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

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

代码如下:

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 <module>
    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()执行前,div为隐藏状态:

clipboard.png

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

clipboard.png

网页源代码如下:

<div class="naddList" coordinate="116.458157,39.92544" locals="1,1142,6830" value="6875660">
    <span class="select" k="select">xxxxxxxx</span>
    <span class="place-selected" k="desc">( 北京-朝阳-东大桥 )</span>
    <span class="select nedit" k="update">修改</span>
    <span class="select nadd" k="new">新增</span>
    <p class="disJob">
        该职位将显示在<span locals="1,1142,6830" class="bold">北京-朝阳-东大桥</span>下。
        <a href="javascript:void(0);" class="noactive active1">点击发布到其他地区</a>
    </p>
    <div style="display: block;" class="naddList pf18 clear">
        <div class="contedit zidx91">
            <div k="1" class="selestyle" id="seleCompCity">北京</div>
            <div style="display: none;" id="divCompCity" class="setbox">
                <div class="tagslist">
                </div>
            </div>
        </div>
        <div class="contedit zidx92">
            <div k="1142" id="seleCompArea" class="selestyle">朝阳</div>
            <div style="display: none;" id="divCompArea" class="setbox">
                <ul class="citylist">
                    <li><a k="1142" href="javascript:void(0);">朝阳</a></li><li><a k="1143" href="javascript:void(0);">海淀</a></li><li><a k="1138" href="javascript:void(0);">东城</a></li><li><a k="1139" href="javascript:void(0);">西城</a></li><li><a k="1140" href="javascript:void(0);">崇文</a></li><li><a k="1141" href="javascript:void(0);">宣武</a></li><li><a k="1144" href="javascript:void(0);">丰台</a></li><li><a k="1147" href="javascript:void(0);">通州</a></li><li><a k="1145" href="javascript:void(0);">石景山</a></li><li><a k="6808" href="javascript:void(0);">房山</a></li><li><a k="1150" href="javascript:void(0);">昌平</a></li><li><a k="6809" href="javascript:void(0);">大兴</a></li><li><a k="6810" href="javascript:void(0);">顺义</a></li><li><a k="6811" href="javascript:void(0);">密云</a></li><li><a k="6812" href="javascript:void(0);">怀柔</a></li><li><a k="6813" href="javascript:void(0);">延庆</a></li><li><a k="6814" href="javascript:void(0);">平谷</a></li><li><a k="6815" href="javascript:void(0);">门头沟</a></li><li><a k="11362" href="javascript:void(0);">燕郊</a></li><li><a k="1146" href="javascript:void(0);">北京周边</a></li><li><a k="-1" href="javascript:void(0);">[不限]</a></li></ul>
            </div>
        </div>
        <div class="contedit zidx93">
            <div id="seleCompCirc" class="selestyle" k="6830" style="display: block;">东大桥</div>
            <div k="-1" id="divCompCirc" class="setbox" style="display: none;">
                <ul class="citylist">
                    <li><a k="1195" href="javascript:void(0);">国贸</a></li><li><a k="6834" href="javascript:void(0);">CBD </a></li><li><a k="5785" href="javascript:void(0);">大望路</a></li><li><a k="5786" href="javascript:void(0);">四惠</a></li><li><a k="5779" href="javascript:void(0);">双井</a></li><li><a k="1199" href="javascript:void(0);">劲松</a></li><li><a k="5780" href="javascript:void(0);">潘家园</a></li><li><a k="5990" href="javascript:void(0);">广渠门</a></li><li><a k="5988" href="javascript:void(0);">建外大街</a></li><li><a k="6830" href="javascript:void(0);">东大桥</a></li><li><a k="5826" href="javascript:void(0);">工体</a></li><li><a k="6017" href="javascript:void(0);">朝外大街</a></li><li><a k="1202" href="javascript:void(0);">京广桥</a></li><li><a k="7559" href="javascript:void(0);">团结湖</a></li><li><a k="5999" href="javascript:void(0);">朝阳公园</a></li><li><a k="1207" href="javascript:void(0);">呼家楼</a></li><li><a k="1198" href="javascript:void(0);">三里屯</a></li><li><a k="6001" href="javascript:void(0);">麦子店</a></li><li><a k="1200" href="javascript:void(0);">燕莎</a></li><li><a k="1194" href="javascript:void(0);">三元桥</a></li><li><a k="7560" href="javascript:void(0);">国展</a></li><li><a k="7561" href="javascript:void(0);">西坝河</a></li><li><a k="7566" href="javascript:void(0);">[不限]</a></li></ul>
            </div>
        </div>                                <div class="contedit">
        <input type="button" value="确定" class="btnsure">
    </div>
    </div>
</div>

还想过使用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)

命令执成功但是并没有修改 <div id="seleCompCirc" class="selestyle" k="6830" style="display: block;">东大桥</div> 的值,本人不懂JS,Orz。

阅读 21.9k
1 个回答
✓ 已被采纳新手上路,请多包涵

问题已经解决:

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏