Tiktok: Selenium unable to find upload area

Question:

As the title suggests, my bot is unable to find the upload area on the tiktok website.

driver.get("https://www.tiktok.com/upload/")
time.sleep(5)
upld = driver.find_element(By.XPATH, "//*[@id='root']/div/div/div/div/div[2]/div[1]/div/div/div[4]/button")
upld.send_keys(r"C:UsersMariusfinal.mp4")

The error I am getting while running the code is:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='root']/div/div/div/div/div[2]/div[1]/div/div/div[4]/button"}
  (Session info: chrome=103.0.5060.134)
Stacktrace:
Backtrace:
        Ordinal0 [0x01155FD3+2187219]
        Ordinal0 [0x010EE6D1+1763025]
        Ordinal0 [0x01003E78+802424]
        Ordinal0 [0x01031C10+990224]
        Ordinal0 [0x01031EAB+990891]
        Ordinal0 [0x0105EC92+1174674]
        Ordinal0 [0x0104CBD4+1100756]
        Ordinal0 [0x0105CFC2+1167298]
        Ordinal0 [0x0104C9A6+1100198]
        Ordinal0 [0x01026F80+946048]
        Ordinal0 [0x01027E76+949878]
        GetHandleVerifier [0x013F90C2+2721218]
        GetHandleVerifier [0x013EAAF0+2662384]
        GetHandleVerifier [0x011E137A+526458]
        GetHandleVerifier [0x011E0416+522518]
        Ordinal0 [0x010F4EAB+1789611]
        Ordinal0 [0x010F97A8+1808296]
        Ordinal0 [0x010F9895+1808533]
        Ordinal0 [0x011026C1+1844929]
        BaseThreadInitThunk [0x7615FA29+25]
        RtlGetAppContainerNamedObjectPath [0x77847A9E+286]
        RtlGetAppContainerNamedObjectPath [0x77847A6E+238]

I’ve tried running the bot on my other device and it worked with no flaws, but it doesnt seem to work when I try it with selenium for some reason. The chrome version is the same on all devices and on selenium as well.

I’ve tried css-selector and class find methods, but they still return no result.

Any help is appreciated.

Asked By: mariusaf

||

Answers:

As the locator strategies worked on your other device presumably the locator is flawless but ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='root']/div/div/div/div/div[2]/div[1]/div/div/div[4]/button"))).send_keys("C:UsersMariusfinal.mp4")
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Answered By: undetected Selenium

Try this:

# There is an iframe on the page...
iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(iframe)

# Wait until page loads...
time.sleep(3)

# Select the input file and send the filename...
upload = driver.find_element(By.CSS_SELECTOR, 'input[type="file"]')
video_path = 'C:/Users/my_video.mp4'
upload.send_keys(video_path)
Answered By: gdrafael

Because the content you are looking for in within an IFrame, Selenium XPath searches, unlike Google Chrome’s Dev Tool XPath seaches, will not go into the IFrame. For this reason, all you need to do is switch iFrames and the code should work as expected.

iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(iframe)

Note: WebDriverWait will do nothing to fix the problem as the page has already fully loaded.

Answered By: William Kaiser