Selenium cant find element with xpath or ClassName or Css Selector (Python)

Question:

Hello I am trying to click The no thanks button on google look : enter image description here

The full element of the button is this

<button aria-label="No thanks" class="M6CB1c rr4y5c" jsname="ZUkOIc" jslog="71121; track:click;" data-dismiss="n">No thanks</button>

I tried to select it with the xpath like this
EDIT: I have a wait element up in the code here Wait = WebDriverWait(driver,60)

driver.get("https://www.google.com/")

Wait = WebDriverWait(driver,60)

buttonSignIn = "//button[@class='M6CB1c rr4y5c'][1]/@class"

Wait.until(EC.visibility_of_any_elements_located((By.XPATH,buttonSignIn)))

buttonSign = driver.find_elements(By.XPATH,buttonSignIn)

print(buttonSign.get_attribute("innerHTML"))

I also before tried to select it with Css like this

buttonSignIn = ".M6CB1c.rr4y5c"

Wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,buttonSignIn)))

buttonSign = driver.find_element(By.CSS_SELECTOR,buttonSignIn)

print(buttonSign.get_attribute("innerHTML"))

I also tried to select it with Class Name like this

buttonSignIn = "M6CB1c rr4y5c"

Wait.until(EC.visibility_of_element_located((By.CLASS_NAME,buttonSignIn)))

buttonSign = driver.find_element(By.CLASS_NAME,buttonSignIn)

print(buttonSign.get_attribute("innerHTML"))

I followed the other stuff from stack overflow that had my problem. WHat am i doing wrong

Asked By: theonedeveloper

||

Answers:

The element is inside an iframe, In order to reach the element, you need to switch to iframe first.

Use WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
Then WebDriverWait() and wait for element_to_be_clickable(). you can use below css selector to uniquely identify the element.

driver.get("https://www.google.com/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name='callout']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.M6CB1c.rr4y5c"))).click()

Import below libraries.

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