在同一个页面同一个步骤,如果使用“wait”会得到错误信息 “NoSuchElementException:消息:无法找到名称 == rw 的元素”
如果使用“switch_to_frame”将成功切换框架..
为什么会有不同?
1)
wait = WebDriverWait(driver, 300)
wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_name('rw')))
2)
driver.switch_to_frame('rw')
3)
class cm(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie()
self.driver.implicitly_wait(30)
self.base_url = "http://mytestweb.com"
def testcm(self):
driver = self.driver
driver.maximize_window()
self.driver.get(self.base_url)
wait = WebDriverWait(driver, 30)
self.main_wh = driver.window_handles
## wait.until(EC.invisibility_of_element_located((By.ID,'Frame2')))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,'rw')))
## driver.switch_to_frame('rw')
如果我尝试使用 3) 将收到超时消息
最佳答案
最有可能的是名称为 rw 的元素,它实际上需要时间来加载,但在查找发生之前没有正确加载。基本上,您是在预期条件出现之前寻找相同的元素。更好的实现如下:
wait = WebDriverWait(driver, 300)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,'rw')))
参见 doc
关于用于框架的python selenium "Explicit Waits",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30148189/