1

Hi I am fairly new to selenium .Can somebody please suggest on how to locate and element inside iframe asi am getting error below.

I am trying to implement an Automation Script on Salesforce Pardot page and there are 2 iframes and i want to access Button on 1st iframe Tag and click on same.

Error:- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[starts-with(@class,'slds-button_reset')]"} (Session info: chrome=80.0.3987.122)

Screenshot for DOM Button Element

Screenshot for DOM Button Element along with Iframe tag

Code Written Previously

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': r'C:\Pardot'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path="D:\XXX XXXX\XXXX\drivers\chromedriver.exe", options=chrome_options)
driver.get('https://pi.pardot.com/engagementStudio/studio#/15627/reporting')
user_name = driver.find_element_by_css_selector('#email_address')
user_name.send_keys('XXXXXXXXXXXXXXXXXXX')
password = driver.find_element_by_css_selector('#password')
password.send_keys('XXXXXXXXXXXXXXXXX)
submit_button = driver.find_element_by_css_selector('input.btn')
submit_button.click()
iframe_list =  driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(iframe_list[0])
driver.find_element_by_xpath("//*[starts-with(@class,'slds-button_reset')]")
driver.close()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Vipul_Garg
  • 43
  • 1
  • 8
  • Please add the code you have tried so far. – 0buz Mar 02 '20 at 13:20
  • @Vipul_21 I am reverting back the changes for this time. If your requirement have changed, feel free to raise a new question as per your new requirement. Stackoverflow volunteers will be happy to help you out. – undetected Selenium Mar 03 '20 at 06:49

4 Answers4

1

As the the desired element is within an <iframe> so to invoke click() on the element 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#content-frame")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-qa='reporting-filter-trigger-toggle'][data-ember-action]"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='content-frame']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-qa='reporting-filter-trigger-toggle' and @data-ember-action]"))).click()
      
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi @Debanjan B thanks for your response, Button got clicked but I need to change value of span element to This Month.I have tried adding below line of code but no luck. `WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value(text_='This Month',locator=(By.CSS_SELECTOR,"span[data-qa='reporting-filter-trigger-value']")))` – Vipul_Garg Mar 03 '20 at 06:36
  • **for SS from DOM element along with dropdown List appear on clicking button** Please refer to Question Post Above I have added Just now. – Vipul_Garg Mar 03 '20 at 06:41
  • @Vipul_21 That should be a separate question altogether. Can you raise a new question as per your new requirement please? – undetected Selenium Mar 03 '20 at 06:47
  • can you Pls check on this post as I have posted separate Question for the same. https://stackoverflow.com/questions/60501908/how-to-access-span-element-under-a-dropdown-list-w-r-t-python-selenium-script – Vipul_Garg Mar 03 '20 at 07:20
0

Try this : //li[@id='ember943']

0

You need to get iframe tag first and switch driver to it like following.

driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) assuming that driver is a healthy instance of webdriver. To continue with the default content do driver.switch_to.default_content()

Please refer this link

Lee Bin
  • 49
  • 3
  • 10
0

I hope that you're switching to the correct Iframe to fetch the element... Please see the example below

driver.switchTo().frame("content-frame")
driver.findElement(By.xpath("//div[@id='ember740']/div[2]/div/ol/li/div/button")
driver.switchTo().defaultContent()