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 識別它,因為它是一個彈出視窗?這是檢查元素
檢查元素
截屏
uj5u.com熱心網友回復:
右鍵單擊元素并轉到復制 > 復制(完整)xpath。
然后使用:
from selenium.webdriver.common.by import By
driver.find_elements(By.XPATH, 'xpath_value_here')
編輯以反映“復制 xpath”和“復制完整 xpath”之間的區別;事實上,根據下面的評論,復制完整的 xpath 是正確的答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482290.html
