如何使用python在iframe中找到按钮的XPath? [英] How to find XPath for button within iframe using python?

查看:48
本文介绍了如何使用python在iframe中找到按钮的XPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 iframe 中有以下 html 对象:

I have the following html object inside an iframe:

提交"按钮的 html 代码

我需要找到它的 XPath 才能单击提交"按钮但找不到它.XPath 助手只显示//iframe".

I need to find it's XPath in order to click on the "SUBMIT" button but cannot find it. XPath helper only shows "//iframe".

到目前为止,我已经尝试过:

So far, I've tried:

submit = driver.find_elements_by_xpath("//iframe[@id='btnSubmit']")
submit.click

推荐答案

frameiframe 内的所有内容都不能在不切换到 的情况下访问iframe/frame.所以首先切换到frame内容:

All content, which is inside a frame or iframe cannot be accesed without switching to iframe/frame. So firstly switch to frame content:

driver.switch_to.frame(driver.find_element_by_name("frame_name"))

driver.switch_to.frame(driver.find_element_by_xpath("//xpath/to/frame"))

然后你可以找到你的提交按钮并点击它:

Then you can locate your submit button and click on it:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='btnSubmit']"))).click()

像这样切换到默认内容:

Than switch to default content like this:

driver.switch_to.default_content()

注意:你必须添加一些导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

这篇关于如何使用python在iframe中找到按钮的XPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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