如何使用python硒单击iframe内的按钮 [英] how to click button which is inside iframe using python selenium

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

问题描述

他是我的样本片段.我想使用python硒单击按钮1034-btnIconEl.

He is my sample snippet. i want to click the button-1034-btnIconEl using python selenium.

<html>
<body>
<div class="x-container x-border-item x-box-item x-container-default x-layout-fit" id="iframes" >
<iframe ></iframe>
<iframe class="x-component x-fit-item x-component-default" frameborder="0"  id="rpIFrame-1239">
 <html>
 <body>
 <div> .....many divs 
  <div>
   <a><span><span id="button-1034-btnIconEl"></span></span></a>
  </div>
 </div>
 </body>
 </html>
</iframe>

我尝试过

 driver.switch_to.frame(1)
 driver.find_element(By.XPATH, "//span[contains(@id,'button-1034-btnIconEl')]").click()

但得到

"NoSuchElementException:没有这样的元素:"

"NoSuchElementException: no such element:"

请帮助我进行遍历.

推荐答案

id button-1034-btnIconEl 的按钮上的click()为所需元素位于<iframe>中,因此您必须:

To click() on the button with id as button-1034-btnIconEl as the the desired element is within an <iframe> so you have to:

  • 诱导 WebDriverWait 以使所需的框架可用并切换到.
  • 诱导 WebDriverWait 以使所需的元素可点击.
  • 您可以使用以下定位器策略:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use either of the following Locator Strategies:

  • 使用CSS_SELECTOR:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.x-component.x-fit-item.x-component-default[id^='rpIFrame-']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a > span > span[id^='button'][id$='btnIconEl']"))).click()

  • 使用XPATH:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='x-component x-fit-item x-component-default' and starts-with(@id, 'rpIFrame-')]")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a/span/span[starts-with(@id, 'button') and contains(@id, 'btnIconEl')]"))).click()
    

  • 此处您可以在在下面的方法中处理#document的方法找到相关的讨论iframe

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

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