使用 selenium 从弹出 window 中按下一个按钮

Press a button from popup window with selenium

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()

所以我有这段代码,我想做的是按下屏幕截图上的全部接受按钮,但我认为代码无法通过 ID 识别它,因为它有点像弹出窗口 window? 这是检查元素

Inspect Element

Screenshot

Right-click 在元素上并转到复制 > 复制(完整)xpath。

然后使用:

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

编辑以反映 'copy xpath' 和 'copy full xpath' 之间的区别;事实上,根据以下评论,复制完整的 xpath 是正确的答案。