I am running selenium scripts using python on a variety of different sites using the same iframe
. On some sites, I'm able to run the full code successfully, however I've come across a site where I am running the same code within the iframe
but am getting an error that says it's unable to find the element.
I've tried different selectors but the same error persists. Keep in mind, this same code is running successfully on other sites with this same iframe.
Here is the html structure of the iframe
:
<iframe class="zoid-component-frame zoid-visible" frameborder="0" allowtransparency="true" name="xcomponent__div_renderer__latest__pmrhk2leei5cendcgi..." title="ShopRunner Love it. Get it." scrolling="no" style="background-color: transparent;" src="https://content.shoprunner.com/components/divRenderer/index.html?env=prd&uid=b9695b75b4&logLevel=error&version=latest&xcomponent=1"></iframe>
Here is the structure of the button:
<button aria-label="Sign up free for ShopRunner." class="sr-button bn bg-transparent underline ph0" type="button" data-trigger="learnMore">Sign Up FREE</button>
This is the version of the code that's working on other sites:
# switch to the context of the browser
browser.switch_to.default_content()
# wait until iframe loads and then switch to the context of the iframe
browser.switch_to.frame(browser.find_element_by_class_name('zoid-component-frame'))
# wait for the learn more button to be clickable and then click
browser.find_element_by_css_selector("button[data-trigger='learnMore']").click()
# switch to the context of the modal
browser.switch_to.default_content()
# pause on learn more modal (for visual inspection) and then close modal
time.sleep(2)
browser.find_element_by_id('sr_header_close').click()
These are some other selectors I've tried with the same error:
browser.find_element_by_xpath("//*[contains(text(), 'Sign Up FREE')]").click()
browser.find_element_by_css_selector(".sr-button.bn.bg-transparent.underline.ph0").click()
I've also tried adding a wait statement between switching to the iframe and finding the css selector and I've tried using an ActionChain
to find the element:
button = browser.find_element_by_css_selector("button[data-trigger='learnMore']")
browser.implicitly_wait(10)
ActionChains(browser).move_to_element(button).click(button).perform()
I am expecting to be able to click on the button but am getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[data-trigger='learnMore']"}