python selenium中的.find_element()出现问题

ijnw1ujt  于 2023-02-04  发布在  Python
关注(0)|答案(1)|浏览(205)

我需要找到“上传”按钮,并输入上传视频。下面是标记的元素和我的代码。Input fieldUpload button

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

def test():
    driver = webdriver.Firefox()
    upload_url = "https://www.tiktok.com/upload?lang=en-EN"
    login_url = "https://www.tiktok.com/login"
    print("Log in manually and press ENTER", end = '')
    time.sleep(5)
    driver.get(login_url)
    driver.implicitly_wait(10)
    input()
    print("Loading upload page...")
    driver.get(upload_url)
    driver.implicitly_wait(10)
    path = ".../yt_to_tt_uploader/yt_videos/YMbO9YYzvVw.mp4"
    upload = driver.find_element(By.XPATH, "//input[@type='file']")
    upload.send_keys(path)
    upload_button = driver.find_element(By.XPATH, "//button[class='css-y1m958']")
    upload_button.click()

这是引发错误

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@type = 'file']

我试着通过XPATH,CLASS_NAME,文本找到它,但也许我错了,我需要在字段中上传视频并点击“上传”

7rtdyuoh

7rtdyuoh1#

Selenium无法在您的代码中找到此元素//input[@type='file']。请尝试使用显式等待,看看它是否有效,下面的代码供您参考:

# wait 20 seconds before looking for element
upload = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//input[@type='file']")))

如果使用presence_of_element_located不起作用,那么尝试visibility_of_element_located

# wait 20 seconds before looking for element
upload = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,"//input[@type='file']")))

需要导入语句:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

我发现另一个XPath中还有一个问题,缺少@
//button[class='css-y1m958']变更为//button[@class='css-y1m958']

相关问题