用于在python,硒中切换帧的函数 [英] function for switching frames in python, selenium

查看:65
本文介绍了用于在python,硒中切换帧的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种功能,可以更轻松地在两个帧之间切换.现在,每次需要在帧之间切换时,我都会通过以下代码进行操作:

I'm looking for a function that makes it easier to switch between two frames. Right now, every time I need to switch between frames, I'm doing this by the following code:

driver.switch_to.frame(driver.find_element_by_css_selector("frame[name='nav']"))

driver.switch_to.frame(driver.find_element_by_css_selector("frame[name='content']"))

我的目标是获得一个仅接受参数即可更改nav或content的函数,因为其余的基本相同.

My goal is to get a function that takes an argument just to change nav or content since the rest is basically the same.

我已经尝试过的是:

def frame_switch(content_or_nav):
x = str(frame[name=str(content_or_nav)] #"frame[name='content_or_nav']"
driver.switch_to.frame(driver.find_element_by_css_selector(x))

但这给我一个错误

 x = str(frame[name=str(content_or_nav)]
                  ^

SyntaxError:语法无效

SyntaxError: invalid syntax

推荐答案

此方式的编写方式是,尝试将CS​​S代码解析为Python代码.你不要那个.

The way this is written, it's trying to parse CSS code as Python code. You don't want that.

此功能合适:

def frame_switch(css_selector):
  driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))

如果您只是尝试基于name属性切换到框架,则可以使用以下方法:

If you are just trying to switch to the frame based on the name attribute, then you can use this:

def frame_switch(name):
  driver.switch_to.frame(driver.find_element_by_name(name))

要切换回主窗口,您可以使用

To switch back to the main window, you can use

driver.switch_to.default_content()

这篇关于用于在python,硒中切换帧的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆