Home > other >  Unable to locate 'No thanks' and Sign in element on google home page at "https://www.
Unable to locate 'No thanks' and Sign in element on google home page at "https://www.

Time:12-26

Code used in VISUAL STUDIO CODE:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.co.in/")
driver.maximize_window()
time.sleep(3)
driver.find_element(By.XPATH,"/html/body/div/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button").click()

enter image description hereError getting

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button"}
  (Session info: chrome=108.0.5359.125)
Stacktrace:
Backtrace:
    (No symbol) [0x005AF243]
    (No symbol) [0x00537FD1]
    (No symbol) [0x0042D04D]
    (No symbol) [0x0045C0B0]
    (No symbol) [0x0045C22B]

enter image description here

CodePudding user response:

Those elements are in an iframe, so you need to switch to the frame first, and then click a button in there:

driver.switch_to.frame("callout")
driver.find_element("css selector", '[aria-label="No thanks"]').click()

CodePudding user response:

I can't see "No Thanks" on that page, but the "Login" button can be accesed there as following:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

url = "https://www.google.co.in/"
driver.get(url)

wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@href,'ServiceLogin')]"))).click()

I used XPath here. However this can be done with CSS Selectors as well

  • Related