Home > Enterprise >  locating an element on an iframe within an iframe
locating an element on an iframe within an iframe

Time:10-23

Hi I need some help with locating the button on an iframe which I think is within another iFrame. I tried everythingenter image description here that I could find but got no success. I can't copy-paste the whole code but have attached the screenshot that explains the tree.

CodePudding user response:

Try to enter in first iframe to enter the second after.

try this code

iframe = navegador.find_element_by_xpath("XpathOf1frame")    
navegador.switch_to.frame(iframe)
iframe2 = navegador.find_element_by_xpath("XpathOf2frame")    
navegador.switch_to.frame(iframe2)
navegador.find_element_by_xpath('XpathOfButton').click()

CodePudding user response:

Please try this:

driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe.swg-dialog"))
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe[src*='publicatioId=barpage.com']"))
driver.find_element(By.CSS_SELECTOR, div[data-test-id='close-button']).click()

When you finished working with elements inside the iframe don't forget to switch back to the default content with

driver.switch_to.default_content()

In case you prefer using XPath, not CSS Selectors, the above code can be easily converted to XPath expressions

driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[contains(@class,'swg-dialog')]"))
driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[contains(@src,'publicatioId=barpage.com')]"))
driver.find_element(By.XPATH, //div[@data-test-id='close-button']).click()

Probably you will have to add waiting until the elements are available etc. In this case WebDriverWait should be used

  • Related