发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!