0

When I enter the google website, this small login window appears:

the tab that i tryind dimiss

I'm trying to dismiss it using selenium with pyhton

Here is the code i am using:

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

driver = webdriver.Chrome()

driver.get('https://www.google.com.br')
driver.maximize_window()

# wait
wait = WebDriverWait(driver, 10)
not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button')))

# click
not_now_button.click()

I get the error below:

Traceback (most recent call last):
  File "C:\Users\sec-info\Desktop\python\googlr.py", line 13, in <module>
    not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button')))
  File "C:\Users\sec-info\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Backtrace:
    (No symbol) [0x0037DCE3]
    (No symbol) [0x003139D1]
    (No symbol) [0x00224DA8]
    (No symbol) [0x0025019F]
    (No symbol) [0x002503AB]
    (No symbol) [0x0027EE62]
    (No symbol) [0x0026AF14]
    (No symbol) [0x0027D57C]
    (No symbol) [0x0026ACC6]
    (No symbol) [0x00246F68]
    (No symbol) [0x002480CD]
    GetHandleVerifier [0x005F3832+2506274]
    GetHandleVerifier [0x00629794+2727300]
    GetHandleVerifier [0x0062E36C+2746716]
    GetHandleVerifier [0x00426690+617600]
    (No symbol) [0x0031C712]
    (No symbol) [0x00321FF8]
    (No symbol) [0x003220DB]
    (No symbol) [0x0032C63B]
    BaseThreadInitThunk [0x76CBFA29+25]
    RtlGetAppContainerNamedObjectPath [0x77E57A9E+286]
    RtlGetAppContainerNamedObjectPath [0x77E57A6E+238]

Python: 3.11.2 Selenium: 4.8.2 Chromedriver: 111.0.5563.64

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Are you sure the code you have shared here is the code you executed? In the screenshot which you have provided, I see the browser is in incognito mode. However, I do not see you setting the driver to incognito mode in your code. – Shawn Mar 09 '23 at 15:33
  • Yes, i only used de incognito mode to take the screenshot – exu_caveirinha Mar 09 '23 at 17:01

3 Answers3

0

Right-click > Inspect

Then use the simplest css (that xpath looks overly complicated)

If you could post the html, I could help

In Chrome, press F12 > Elements > Ctrl+F

Type in the css from above

button.M6CB1c.rr4y5c[aria-label='Agora não']

check if Chrome sees the element

gords
  • 51
  • 7
0

You could use xpath or css selector to locate that particular button.

You need to switch to iframe since the button you're trying to click is inside an iframe.

Use below code to switch to iframe:

driver.switch_to.frame('callout') 

Using xpath :

not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='M6CB1c rr4y5c' and @aria-label='Agora não']")))

Using css selector:

not_now_button =  wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.M6CB1c.rr4y5c[aria-label='Agora não']"))) 

Once you're done clicking the button, do not forget to switch back to default content using below code :

driver.switch_to.default_content()

Your code should be as below:

driver.switch_to.frame('callout')
not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='M6CB1c rr4y5c' and @aria-label='Agora não']")))
not_now_button.click()
driver.switch_to.default_content()

If you want to use css selector instead of xpath to locate the button, use the css locator strategy as shown above .

Harish
  • 306
  • 1
  • 4
  • 14
0

The element...

NoThanks

...is within an <iframe>

So to click on the element to dismiss the popup 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:

    driver.get("https://www.google.com.br/")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name=callout]")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='No thanks']"))).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