python selenium“显式等待”框架

use*_*400 5 python selenium

在同一页面同一步骤中,如果使用“wait”将收到错误消息“NoSuchElementException: Message: Unable to find element with name == 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')))
Run Code Online (Sandbox Code Playgroud)

2)

driver.switch_to_frame('rw')
Run Code Online (Sandbox Code Playgroud)

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')
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用 3) 将收到超时消息

Sai*_*fur 7

最有可能的是名称为rw 的元素(实际上需要一些时间来加载)在查找发生之前未正确加载。基本上,您正在寻找在预期条件出现之前相同的元素。更好的实现如下:

wait = WebDriverWait(driver, 300)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,'rw')))
Run Code Online (Sandbox Code Playgroud)

请参阅文档

  • 哇,还没有注意到这个“frame_to_be_available_and_switch_to_it”预期条件。学到了新东西。 (2认同)