function for switching frames in python, selenium(python, selenium中用于切换帧的函数)
问题描述
我正在寻找一种可以更轻松地在两个帧之间切换的功能.现在,每次我需要在帧之间切换时,我都通过以下代码来做到这一点:
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']"))
我的目标是获得一个函数,该函数接受一个参数来更改导航或内容,因为其余部分基本相同.
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
推荐答案
这个写法,它试图将 CSS 代码解析为 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, selenium中用于切换帧的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python, selenium中用于切换帧的函数
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
