最近学习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。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
问题已经解决:
通过
driver.find_element_by_xpath(".//*[@id='tr_city']/td/p/p/p[3]").click()定位成功