gpt4 book ai didi

python - 为什么 Selenium 不点击?(Python)

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:43 25 4
gpt4 key购买 nike

前几天,我开始使用 Selenium 库在 YouTube 上按下按钮,一切都很好,但前几天,由于某种原因,它停止按下按钮,我没有更改任何内容,一切都完全相同,而当我在另一台电脑上检查代码时,一切都很好,最重要的是,他甚至没有给我错误并假装一切顺利!在这种情况下该怎么办,我会很高兴每一个答案! (用Python工作)

from selenium import webdriver
import time

url = 'url video'
FILE_NAME_PROFILE = "C:/Users/xxx/AppData/Local/Google/Chrome/User Data"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=" + FILE_NAME_PROFILE)
driver =webdriver.Chrome(executable_path="C:/Users/xxx/Downloads/chromedriver_win32(1)/chromedriver.exe",chrome_options=options)
driver.get(url)
time.sleep(10)
like = driver.find_element_by_xpath('//yt-icon[@class="style-scope ytd-toggle-button-renderer"]')
like.click()
driver.quit()

最佳答案

尝试这样调整:

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait ##new line of code

url = 'url video'
FILE_NAME_PROFILE = "C:/Users/xxx/AppData/Local/Google/Chrome/User Data"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=" + FILE_NAME_PROFILE)
driver =webdriver.Chrome(executable_path="C:/Users/xxx/Downloads/chromedriver_win32(1)/chromedriver.exe",chrome_options=options)
driver.get(url)
time.sleep(10)

inp_xpath_search = '//yt-icon[@class="style-scope ytd-toggle-button-renderer"]'

like = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))

like.click()
time.sleep(2)

driver.quit()

关于python - 为什么 Selenium 不点击?(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com