python selenium框架_如何正确等待python-selenium中的框架可用?

我有一个相当复杂的网页设置我需要测试,包含嵌套框架.

在实际问题中,selenium代码正在加载包含框架的新网页内容,我想切换到该框架.为了避免任何明确的等待,我尝试了以下代码片段:

self.driver.switch_to_default_content()

WebDriverWait(self.driver, 300).\n until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'frame1')))

WebDriverWait(self.driver, 300).\n until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'frame2')))

但是,此代码段始终失败并导致以下错误:

...

File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 71, in until

value = method(self._driver)

File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/selenium/webdriver/support/expected_conditions.py", line 247, in __call__

self.frame_locator))

File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/selenium/webdriver/support/expected_conditions.py", line 402, in _find_element

raise e

WebDriverException: Message: TypeError: can't access dead object

但是,如果我另外使用睡眠:

time.sleep(30)

self.driver.switch_to_default_content()

WebDriverWait(self.driver, 300).\n until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'frame1')))

WebDriverWait(self.driver, 300).\n until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'frame2')))

selenium能够找到框架内的框架并切换到它.看起来在错误的情况下,selenium切换到’frame1’而’frame2’尚未加载,但’frame2’被加载到’frame1’的某个其他实例中,或者selenium无法识别(可能是bug?).所以现在selenium在某个’frame1’里面,并且由于某些原因没有意识到’frame2’已被加载.

我可以解决这个问题的唯一方法(不使用长时间睡眠)就是使用这段丑陋的代码:

mustend = time.time() + 300

while time.time() < mustend:

try:

self.driver.switch_to_default_content()

self.driver.switch_to.frame(self.driver.find_element_by_id("frame1"))

self.driver.switch_to.frame(self.driver.find_element_by_id("frame2"))

break

except WebDriverException as e:

self.log("Sleeping 1 sec")

time.sleep(1)

if time.time() > mustend:

raise TimeoutException

因此,每当我得到一个WebDriverException(死对象)时,我会转到顶层框架并尝试逐帧切换到内部框架.

我可以尝试其他方法吗?

附加信息

> iframe是嵌套的,即’frame2’在’frame1’内.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值