2016-09-21 139 views
0

美好的一天。我试图学习如何在Python上使用Firefox上的Selenium IDE,方法是将我做的任何测试导出到Python 2.7。使用iframe的Python Selenium webdriver

在测试过程中,我遇到了一些问题,其中一个问题是它不能识别2个文本字段,这些字段位于内部iframe中。我在堆栈溢出中发现了一些其他的答案,但我不知道如何将它们应用于我的代码。这是我从Firefox上直接从Selenium IDE导出的内容。我对Python和编程一般也是全新的,所以任何建议也会受到欢迎。

这就是我现在所拥有的:

# -*- 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 StiMPythonWebdriver(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.driver.implicitly_wait(30) 
     self.base_url = "https://webpage.com/" 
     self.verificationErrors = [] 
     self.accept_next_alert = True 

    def test_sti_m_python_webdriver(self): 
     driver = self.driver 
     driver.find_element_by_id("SEARCHS").clear() 
     driver.find_element_by_id("SEARCHS").send_keys("403627") **It inserts a code of a form** 
     driver.find_element_by_id("splitbutton1-button").click() 
     # ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | details403627 | 100000]] 
     # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=details403627 | ]] **It waits a little for the pop up window to open so it can continue the test** 
     driver.find_element_by_id("editButton_textSpan").click() 
     Select(driver.find_element_by_id("status")).select_by_visible_text("Option 1") 
     # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | id=descrpt_ifr | ]] 
     # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | id=tinymce | ]] **Right here at this part it is supposed to select the <p> inside the Iframe and type the following sentence "Canceled"** 


     driver.find_element_by_id("descrpt_ifr").clear() 
     driver.find_element_by_id("descrpt_ifr").send_keys("Canceled") 
     # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=details403627 | ]] 
     driver.find_element_by_id("goButton_textSpan").click()**then it selects a button that exits the pop up** 
+0

你的问题不清楚给我。你想实现什么?你能否分享相关的HTML以及更详细的说明? –

回答

1

如果你想切换到iframe的

driver.switch_to_frame(driver.find_element_by_xpath("//iframe[@id='tinymce ']")) 

如果你想切换到窗口-1似乎默认的活动窗口

driver.switch_to_window(driver.window_handles[-1]) 

完成后不要忘记切换回来。