Pieter de Wit
Pieter de Wit

Reputation: 73

Clicking button via python selenium chrome

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.

Upvotes: 1

Views: 811

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

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

Upvotes: 3

KunduK
KunduK

Reputation: 33384

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()

Upvotes: 1

Related Questions