Home > Enterprise >  Press a button from popup window with selenium
Press a button from popup window with selenium

Time:05-26

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
# Add experimental options to remove "Google Chrome is controlled by automated software" notification
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(r'C:\Users\iwanh\Desktop\Drivers\chromedriver.exe', options=chrome_options)

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

# We use driver.find_element with the help of the By import instead of find_element_by_name or id


accept_all = driver.find_element(By.ID, value="text")
time.sleep(7)
accept_all.submit()

So i have this code, and what i am trying to do is press the accept all button on the screenshot but I think the code doesnt recognize it with the id because it is some what of a popup window? And here is the inspect element

Inspect Element

Screenshot

CodePudding user response:

Right-click on the element and go to Copy > Copy xpath.

Then use:

from selenium.webdriver.common.by import By
    
driver.find_elements(By.XPATH, 'xpath_value_here')
  • Related