1

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&amp;uid=b9695b75b4&amp;logLevel=error&amp;version=latest&amp;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']"}

frianH
  • 7,295
  • 6
  • 20
  • 45
  • Does iframe class name same on all sites? it looks like you are not using full class name - 'zoid-component-frame zoid-visible' while switching to iframe. – Sureshmani Kalirajan Aug 23 '19 at 19:16
  • You're right, I'm only using part of the name, on the other sites it is successful, and on this one it doesn't break until after finding the iframe, there's just something preventing it from finding the element after switching to the iframe on this one that I haven't figured out yet. – user10219220 Aug 25 '19 at 14:59

2 Answers2

0

I see you are getting the exception "NoSuchElementException: Message: no such element: Unable to locate element:"

You can read more on this in this link: NoSuchElementException - Unable to locate element

ANM1996
  • 161
  • 3
  • Thanks for the link, I tried those suggestions but the error persists. Haven't been able to find out why the element can't be found here, it may be something unusual is happening. – user10219220 Aug 25 '19 at 15:02
0

Try switch to iframe with this strategy.

Import this :

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

And try :

WebDriverWait(browser, 20).until(expected_conditions.frame_to_be_available_and_switch_to_it(browser.find_element_by_xpath('//*[@class="zoid-component-frame zoid-visible"]')))

For Sign Up FREE button, please try find by xpath like this :

browser.find_element_by_xpath('//*[@class="sr-button bn bg-transparent underline ph0"]').click()
frianH
  • 7,295
  • 6
  • 20
  • 45