程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

我的机器人似乎无法点击网站按钮

发布于2024-11-10 09:30     阅读(312)     评论(0)     点赞(6)     收藏(5)


我正在尝试使用我的 python 和 selenium 单击按钮,将 AWS Polly 上的对话方法从生成更改为神经。我已尝试使用 x-path 和选择器对元素进行多种变体,但似乎无法使其单击。( https://i.sstatic.net/f5xQRG86.png )( https://i.sstatic.net/mdKYgXHD.png )

以下是代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Configure Chrome WebDriver path
chrome_driver_path = "C:/Users/trave/Downloads/chromedriver-win64/chromedriver-win64/chromedriver.exe"

# Path to Chrome user profile
user_data_dir = "C:/Users/trave/AppData/Local/Google/Chrome/User Data"
profile_dir = "Profile 4"

# Setting up Chrome options
chrome_options = Options()
chrome_options.add_argument(f"--user-data-dir={user_data_dir}")
chrome_options.add_argument(f"--profile-directory={profile_dir}")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")

# Initialize the WebDriver with Chrome options
service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)

try:
    # Open the AWS Polly Synthesize Speech page
    polly_url = "https://us-east-1.console.aws.amazon.com/polly/home/SynthesizeSpeech"
    driver.get(polly_url)

    print("Waiting for the 'Next' button...")
    next_button = WebDriverWait(driver, 15).until(
        EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div[5]/button"))
    )
    next_button.click()
    print("Clicked 'Next' button.")

    print("Waiting for the 'Sign In' button...")
    signin_button = WebDriverWait(driver, 15).until(
        EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div[1]/div[1]/div[4]/div[5]/button"))
    )
    signin_button.click()
    print("Clicked 'Sign In' button.")

    WebDriverWait(driver, 15).until(
        lambda d: d.execute_script('return document.readyState') == 'complete'
    )
    print("Page loaded.")

    # Try interacting with the element using different methods

    # Method 1: Using XPath
    try:
        neural_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[2]/div/div[1]/div/div/div/main/div/div[3]/div/div/div[2]/div/div/div[2]/div/div[1]/div/div[1]/div/div[3]/div/div/div/div/div/div/div[3]/div/span/span/span[1]/input'))
        )
        neural_button.click()
        print("Clicked the 'Neural' button using XPath.")
    except Exception as e:
        print(f"Failed to click using XPath: {e}")

    # Method 2: Using CSS Selector
    try:
        neural_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '#\\36 6-1725057032906-3969'))
        )
        neural_button.click()
        print("Clicked the 'Neural' button using CSS Selector.")
    except Exception as e:
        print(f"Failed to click using CSS Selector: {e}")

    # Method 3: Using JavaScript
    try:
        neural_button = driver.find_element(By.CSS_SELECTOR, '#\\36 6-1725057032906-3969')
        driver.execute_script("arguments[0].click();", neural_button)
        print("Clicked the 'Neural' button using JavaScript.")
    except Exception as e:
        print(f"Failed to click using JavaScript: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    #Close the browser after a delay
    time.sleep(5)
    driver.quit()

运行此程序后,我没有收到任何错误消息,也没有打印任何内容。我终其一生都无法弄清楚原因。


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2045418/192444a093545895409b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)