我有以下Selenium Python脚本(python3的完整工作示例):
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Test1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://ironspider.ca/"
self.verificationErrors = []
self.accept_next_alert = True
def test_1(self):
driver = self.driver
driver.get(self.base_url + "/frames/frames_example1/advanced.htm")
driver.switch_to.frame("content")
self.assertEqual("The Eve of the War", driver.find_element_by_css_selector("h2").text)
driver.switch_to.frame("menu")
driver.find_element_by_link_text("Chapter 2").click()
driver.switch_to.frame("content")
self.assertEqual("The Falling Star", driver.find_element_by_css_selector("h2").text)
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
运行脚本后,收到以下错误:
======================================================================
ERROR: test_1 (__main__.Test1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "E:/Privat/Learn/Selenium/test2.py", line 23, in test_1
driver.switch_to.frame("menu")
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 67, in frame
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: Unable to locate frame: menu
Stacktrace:
at FirefoxDriver.prototype.switchToFrame (file:///C:/Users/dial1/AppData/Local/Temp/tmpyz7v7fdj/extensions/fxdriver@googlecode.com/components/driver-component.js:10729)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/dial1/AppData/Local/Temp/tmpyz7v7fdj/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/dial1/AppData/Local/Temp/tmpyz7v7fdj/extensions/fxdriver@googlecode.com/components/command-processor.js:621)
----------------------------------------------------------------------
Ran 1 test in 34.349s
FAILED (errors=1)
但是,有问题的网页 包含一个名为&#39; menu&#39;的框架,那么为什么Selenium会失败呢?我该如何解决这个问题?
答案 0 :(得分:0)
必须切换回&#39;默认&#39;窗口,看固定代码。但是,这似乎没有记录......
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Test1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://ironspider.ca/"
self.verificationErrors = []
self.accept_next_alert = True
def test_1(self):
driver = self.driver
driver.get(self.base_url + "/frames/frames_example1/advanced.htm")
driver.switch_to.frame("content")
self.assertEqual("The Eve of the War", driver.find_element_by_css_selector("h2").text)
driver.switch_to.default_content()
driver.switch_to.frame("menu")
driver.find_element_by_link_text("Chapter 2").click()
driver.switch_to.default_content()
driver.switch_to.frame("content")
self.assertEqual("The Falling Star", driver.find_element_by_css_selector("h2").text)
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()