简体   繁体   English

来自 python selenium 的 .find_element() 问题

[英]Problem with .find_element() from python selenium

I need to find the "upload" button and input to upload the video.我需要找到“上传”按钮并输入以上传视频。 Below is the markup of the elements and my code.下面是元素的标记和我的代码。 Input field Upload 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()

It's raising error它引发错误

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

I tried to find it by XPATH, CLASS_NAME, text, but maybe i was wrong.我试图通过 XPATH,CLASS_NAME,文本找到它,但也许我错了。 I need to upload video in the field and click "upload"我需要在现场上传视频,点击“上传”

Selenium is not able to locate this element //input[@type='file'] in your code. Selenium 无法在您的代码中找到此元素//input[@type='file'] Try using an explicit wait and see if it works, below code for your reference:尝试使用显式等待,看看它是否有效,下面的代码供您参考:

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

If using presence_of_element_located doesn't work, then try visibility_of_element_located .如果使用presence_of_element_located不起作用,请尝试visibility_of_element_located Below is the code:下面是代码:

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

Import statements required:需要导入语句:

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

I see one more issue in your other XPath, @ is missing:我在你的另一个 XPath 中看到了一个问题, @丢失了:

Change from //button[class='css-y1m958'] to //button[@class='css-y1m958']//button[class='css-y1m958']更改为//button[@class='css-y1m958']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM