'how do i press enter on a cr-button with selenium python

# import the webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import csv
from math import floor

PATH = r'C:\Users\jj\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.get('chrome://settings/clearBrowserData')

at this point, i get a pop-up from google.

I've tried using a bunch of different variations:

1) driver.switch_to.alert.send_keys(Keys.ENTER)
2) driver.switch_to.active_element.send_keys(Keys.ENTER)
3) driver.switch_to.active_element.find_element(By.ID, "clearBrowsingDataConfirm").send_keys(Keys.ENTER)
4) driver.find_element(By.XPATH, '//button[normalize-space(text())="Clear data"]')
   since the actual text has leading and trailing spaces.
5) driver.find_element(By.ID, "clearBrowsingDataConfirm").send_keys(Keys.ENTER)
6) driver.find_element(By.CSS_SELECTOR,"* /deep/ #clearBrowsingDataConfirm").click();

i've also tried pyautogui and using pyautogui.hotkey('enter')

nothing is working. also, why do i need to find an element if it is a cr-button? why can't i just press enter somehow?

i keep getting:

selenium.common.exceptions.NoSuchElementException


Solution 1:[1]

If I understand right, there is a button that says "Clear browsing data" that you are trying to click. To click a button with selenium you use .click().

For example you can try

driver.find_element(By.ID, "clearBrowsingDataConfirm").click()

you can also copy the elements XPATH and use that to locate the element, then do .click().

If that doesn't work, also try

driver.find_element(by=By.ID, value='clearBrowsingDataConfirm').click()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DDUffy