1

I would like to click button "ja ik ga akkoord" on url anwb.nl with python selenium chrome. I have copied the relative xpath but when is use it i keep getting NoSuchElementException. Also id, name, etc no luck

I start with:

   from selenium import webdriver
   from selenium.webdriver.chrome.options import Options
   options = Options()
   driver = webdriver.Chrome(options=options)
   driver.get('https://anwb.nl')

When i inspect the page, xpath of the button gives me:

   //*[@id="accept default level"]

When i use this with ...by_xpath i get NoSuchElementException The code of the button is:

   <button class="btn-decide_link-internal" type="button"
   name="save"
   id="accept default level"> ==$0
   Ja, ik ga akkoord</button>

I tried id (accept def...), name (save), but all nosuchelement

In general i would really like to understand how to interpret the web code in general can solve future problems.

2
  • I opened this page but I don't see this button Commented Sep 17, 2019 at 13:58
  • anwb.nl should get you to the page Commented Sep 17, 2019 at 15:12

2 Answers 2

3

The element with text as Ja, ik ga akkoord 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:

      driver.get("https://www.anwb.nl/");
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='anwb']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-decide_link-internal"))).click()
      
    • Using XPATH:

      driver.get("https://www.anwb.nl/");
      WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src, 'anwb')]")))
      WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@id='accept default level']"))).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
      
    • Browser snapshot:

anwb

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

Sign up to request clarification or add additional context in comments.

2 Comments

Where this Nasdaq come from :(
For newbies to iframe check youtube.com/watch?v=NhRx99uFUNk Helped me to understand better
1

There is an iframe.Induce WebDriverWait and switch to frame first and then click on the button.

EC.frame_to_be_available_and_switch_to_it()

EC.element_to_be_clickable()

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
driver = webdriver.Chrome(options=options)
driver.get('https://anwb.nl')
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME,"iframe")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"accept default level"))).click()

3 Comments

Not always.However in your case there is an iframe which stopping you access the element until you switch it to.
So in general: if I get an unexpected Nosuchelement I can check if the element is in an 'iframe'. In this example it is <iframe src = 272. . . > <br> I will dig in iframe. Wonder if there can be more iframe on one page
Yes that’s correct.Sometimes there can be nested iframe on a page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.