python selenium,当前窗口跳转在返回原页面,cache被清空
需求:
1. 获取页面上指定的多个链接,并依次点击
2. 点击在当前窗口跳转到目标页面之后,“后退”到原始页面
3. 在原始页面上继续点击后续的链接
因为当当前页面发生跳转之后,存在cache中的关于这个页面的元素也被清空,所以参考: http://www.cnblogs.com/harolei/p/3464403.html 这个改了下
但执行到‘line.find_element_by_class_name("result-class").click()’这步是报错,提示:
Traceback (most recent call last):
File "click.py", line 87, in <module>
line.find_element_by_class_name("result-class").click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 255, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 411, in find_element
{"using": by, "value": value})['value']
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 404, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 195, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 170, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"result-class"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/wj/6lkd2_7x0flgks_8ycttq_j00000gn/T/tmpUKXnLL/extensions/[email protected]/components/driver-component.js:10299)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/wj/6lkd2_7x0flgks_8ycttq_j00000gn/T/tmpUKXnLL/extensions/[email protected]/components/driver-component.js:603)
一直没找到解决办法...,原始代码如下:
browser.implicitly_wait(10)
browser.get("http://m.baidu.com")
time.sleep(1)
browser.find_element_by_id("index-kw").send_keys(word)
browser.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/form/div[1]/div/button").click()
'''暂停5s'''
time.sleep(1)
length = len(browser.find_elements_by_class_name("result"))
for i in range(1,length):
lists = browser.find_elements_by_class_name("result")
line = lists[i]
line.find_element_by_class_name("result-class").click()
browser.back()