-1

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>

i tried this

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

but getting

"NoSuchElementException: no such element:"

Please help me to traverse this.

Anu Priya
  • 33
  • 1
  • 1
  • 9
  • try out this answer [link](https://stackoverflow.com/questions/44834358/switch-to-an-iframe-through-selenium-and-python) – bugsb Dec 12 '19 at 10:59

3 Answers3

0

Use the frame id instead of index

driver.switch_to.frame('rpIFrame-1239')
Guy
  • 46,488
  • 10
  • 44
  • 88
0

To switch on the iframe you can use:

driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))

Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
0

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

  • 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:

    • Using 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()
      
    • Using 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()
      

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @AnuPriya The xml and the locators are tested and pretty much in sync. Possibly you made a mistake while handcrafting the HTML. Recheck and retest and let me know the status. – undetected Selenium Dec 12 '19 at 12:25