[英]Using Selenium, Python script to open Firefox, not going to URL provided
[英]How are we going to hit next url using python and selenium IDE
我正在尝试使用python和Selenium IDE来访问下一个URL。
它只显示第一个链接,然后显示下一个URL的错误。我不知道我在这里缺少什么。 谁能帮我吗?
这是我的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#base Url
baseurl="http://www.happytrips.com/"
driver = webdriver.Firefox()
driver.get(baseurl)
driver.implicitly_wait(2)
link_name=driver.find_elements_by_xpath(".//*[@id='container']/div[3]/div/nav/div/ul/li/a")
for tab1 in link_name:
driver.implicitly_wait(3)
tab1_destination=tab1.get_attribute('href')
print tab1_destination
driver.get(tab1_destination)
产量
http://timesofindia.indiatimes.com/
Traceback (most recent call last):
File "happytest1.py", line 13, in <module>
tab1_destination=tab1.get_attribute('href')
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 97, in get_attribute
resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 402, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, 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:8953)
at Utils.getElementAt (file:///tmp/tmpH_Pmgs/extensions/fxdriver@googlecode.com/components/command-processor.js:8546)
at WebElement.getElementAttribute (file:///tmp/tmpH_Pmgs/extensions/fxdriver@googlecode.com/components/command-processor.js:11746)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpH_Pmgs/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpH_Pmgs/extensions/fxdriver@googlecode.com/components/command-processor.js:603)
当获取另一个页面时,所有现有元素都将变为无效。 您需要获取所有URL,然后才能切换到新页面。
因此,添加另一个循环。 第一个循环遍历元素以保存每个URL,第二个循环遍历URL列表而不是元素列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.