使用 Python 和 Selenium 4 进行 Web 自动化时单击按钮没有响应

问题描述 投票:0回答:1

我正在准备一个网络自动化系统,一切进展顺利,但我卡在了图片中的部分,无论我尝试什么方法,它都不起作用我无法点击“新 Google Ads 帐户”按钮,如果您愿意,我将非常高兴可以帮助我。

图片


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException
from time import sleep


#Open ADS Login Page
    driver.get("https://www.youtube.com/ads/")
    
    start_now_button = driver.find_element(By.XPATH, '//*[@id="content"]/div[1]/div/div/a[1]')
    driver.execute_script("arguments[0].click();", start_now_button)
    
    with open("data.txt", "r") as file:
     credentials = file.readline().split(':')
     email = credentials[0].strip()
     password = credentials[1].strip()
    
    email_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="identifierId"]')))
    email_field.send_keys(email)
    sleep(5)
    
    next_button = driver.find_element(By.ID, 'identifierNext')
    driver.execute_script("arguments[0].click();", next_button)
    sleep(5)
    
    password_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')))
    password_field.send_keys(password)
    sleep(5)

    next_button = driver.find_element(By.XPATH, '//*[@id="passwordNext"]')
    driver.execute_script("arguments[0].click();", next_button)
    sleep(5) 
    
    next_button = driver.find_element(By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div/div[2]/div[4]/div[1]/button/div[3]')
    driver.execute_script("arguments[0].click();", next_button)
    sleep(5)
    
    # New Google ADS Account
    element = driver.find_element(By.XPATH, '//button[@class="mdc-button mdc-button--text new-account-button"]//span[text()="New Google Ads Account"]')
    driver.execute_script("arguments[0].click();", element)

我在人工智能的帮助下尝试了很多方法,例如类名xpath等,但没有成功。

python selenium-webdriver
1个回答
0
投票

您尚未考虑 Cookie 通知的可能性。

试试这个:

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
from selenium.webdriver import ChromeOptions

options = ChromeOptions()
options.add_argument("--headless")

with webdriver.Chrome(options=options) as driver:
    driver.get("https://www.youtube.com/ads/")
    
    wait = WebDriverWait(driver, 5)
    selector = By.CSS_SELECTOR, "button.glue-cookie-notification-bar__reject"
    try:
        wait.until(EC.element_to_be_clickable(selector)).click()
    except Exception:
        pass
    selector = By.CSS_SELECTOR, 'a[data-g-cta-name="promote-your-channel"]'
    wait.until(EC.element_to_be_clickable(selector)).click()

注:

我优先使用 CSS 选择器而不是 XPATH 有两个原因。首先是为了速度,其次如果 HTML 结构发生变化,XPATH 可能会变得无效,而 CSS 选择器可能仍然有效

© www.soinside.com 2019 - 2024. All rights reserved.