0

so I'm fairly new to python but I start to understand how things work together, however for this problem I didn't find any solution.

So my problem is I'm making a simple bot with python that can open Youtube and search for specific keywords, Now I have tried the send.keys method and it won't work because I want to use special characters that ChromeWebDriver just can't send due to chromedriver only supports characters in the bmp Problem!

So then I explored my options 1 using GeckoDriver Firefox instead and yeah it worked! however, I couldn't make Firefox undetected as a bot!

option 2 is using the execute_script method with ChromeWebDriver it did work at first with Google search, But then I wanted to use it for Youtube and nothing happens.

I run my script browser works youtube page is loaded but nothing happens the script completes the 20s sleep and exists normally but no input has been made in the search bar or any error in my console just nothing!

maybe the element is not loaded? but I put 20sec sleep after each line of code to make sure the page have enough time to load properly I tried to use Find_by_id | xPath | class_name still nothing

I tried to check if the element is there before executing the input text line and still nothing

Also, I have tried to .click the element before doing text input the search bar is highlighted the click worked but still no text and nothing.

As for the characters I'm trying to input into the search bar are like this:

which can't be sent by the send.key method

My code:

import undetected_chromedriver.v2 as uc
from time import sleep 
import jdk
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.by import By

if __name__ == '__main__':

    driver = uc.Chrome()
    driver.get('https://youtube.com')
    sleep(20)
   
    driver.execute_script("document.getElementById('search').checked = true;")
    print('Searching for Element')
    sleep(20)

    Elem_nt = driver.find_element(By.XPATH, "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form")
    print('Finding Element Now...')
    sleep(20)

    driver.find_element(By.XPATH, "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form").click()
    print('Clickling Element Now')
    sleep(20)
    
    driver.execute_script("arguments[0].value = 'vid_title';", Elem_nt)
    print('input Text Now')
    sleep(20)

Any help would be appreciated, also excuse the terms that I use to explain as I'm new to this I just want to explain everything.

  • If you send normal-alfanumeric characters, does it work? - it's not clear to me, though. – Marco Aurelio Fernandez Reyes Dec 28 '21 at 21:43
  • Yes when I send alfanumeric normal characters it does work with sendkeys method however with java execute_script nothing works even normal characters. – pycodak Dec 28 '21 at 21:50
  • Do you research `execute_script` returns an exception or in which of the `print('...')` prints something? - to me, it does look the page is not loaded, yet or the xpath is not correct.; maybe if you could post this code on Google Colab, I could give it a try - I'm not familiar with Selenium. – Marco Aurelio Fernandez Reyes Dec 28 '21 at 22:02
  • Yes at first I tell it to search for the element by `xPath` and make sure it is there if `True` it will continue to the next line of code as for the `print` i only did it to make sure that each line is executed and not skipped so after each line of code I till it to print something just to make sure it did execute the mainline above it, so the whole general goal is to open a browser load youtube and input text into the youtube search bar. sure i will check it and put it there – pycodak Dec 28 '21 at 22:55

1 Answers1

0

Try this one

import pyperclip

from selenium.webdriver.common.keys import Keys
pyperclip.copy(your_text)
element = driver.find_element(By.CLASS_NAME,"your_class_name")
element.send_keys(Keys.CONTROL + "V")
Ha Luu
  • 11
  • 3