【发布时间】:2015-06-12 11:22:44
【问题描述】:
我正在提取网址每个页面上的第一个“名称”字段:“http://www.srlworld.com/content/65/find-a-lab.html”
for循环运行一次就报错:
File "srl.py", line 40, in <module>
print state.text
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 66, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 404, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 195, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 170, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:8981)
at Utils.getElementAt (file:///tmp/tmpPEHToH/extensions/fxdriver@googlecode.com/components/command-processor.js:8574)
at WebElement.getElementText (file:///tmp/tmpPEHToH/extensions/fxdriver@googlecode.com/components/command-processor.js:11722)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpPEHToH/extensions/fxdriver@googlecode.com/components/command-processor.js:12282)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpPEHToH/extensions/fxdriver@googlecode.com/components/command-processor.js:603)
代码是:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
driver = webdriver.Firefox()
driver.get("http://www.srlworld.com/content/65/find-a-lab.html")
#assert "http" in driver.title
elem = driver.find_element_by_id("country")
#driver.implicitly_wait(5)
all_countries = elem.find_elements_by_tag_name("option")
country = all_countries[1]
print "country value is %s" % country.get_attribute("value")
country.click()
driver.implicitly_wait(2)
state_elem = driver.find_element_by_id("state")
all_states = state_elem.find_elements_by_tag_name("option")
del all_states[0]
for state in all_states:
print "start ",
print state.text
print "state value is %s" % state.get_attribute("value")
state.click()
driver.implicitly_wait(2)
driver.find_element_by_name("go").click()
name = driver.find_element_by_xpath("//div[span='Name'][1]/span/following-sibling::span[2]")
print name.text
print "end ",
print state.text
在运行此脚本时,即使我没有进行任何更改,只运行一次的 for 循环也不会打印最后一个“state.text”。
【问题讨论】:
-
你想展示什么?下拉列表中的状态名称..或任何其他?
-
是的状态名称以及我使用 xpath 提取的第一个“名称”字段。 @SarithaG
-
试试这个 xpath 来获取名字:" .//*[@id='qname'] "。
-
我很容易得到第一个名字......这没有问题......我没有得到的是我的代码没有打印下一个状态,即'Assam'
-
@SarithaG 它打印这个:国家值是印度开始安得拉邦状态值是安得拉邦 AMBICARE CLINICS & DIAGNOSTICS end Traceback(最近一次通话最后):
标签: python selenium web-scraping