Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to write a simple bot for YouTube streams using python and selenium. But there was a problem with the frames

I need the program to select a message entry element, enter the data and send it.
Here is the beginning of my program. After launching chrome, I go manually to YouTube and turn on some stream with a chat.

from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import undetected_chromedriver as uc

chrome_options = Options()
chrome_options.add_argument('--allow-profiles-outside-user-dir')
chrome_options.add_argument('--enable-profile-shortcut-manager')
chrome_options.add_argument(r'user-data-dir=.\User')
chrome_options.add_argument('--profile-directory=Profile 3')
driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
x = int(input())
...

After that, I run the program further, and it should find the chat frame (because I read that there are frames on the pages, and selenium only works with the top one, so you need to select the chat frame, because it has it). But the frame is not located, and after viewing the code I could not find any more names, ids, etc. of the frame.

this is one of the options:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pis = driver.find_element(By.XPATH, '//*[@id="content-pages"]')
print(pis)
driver.switch_to.frame(pis) 

an example of an html page can be found on any YouTube stream

>Solution :

You need use xpath of iframe itself, not the xpath of element contaning iframe.
So try using

pis = driver.find_element(By.XPATH, '//*[@id="chatframe"]')

You can include iframe tag instead of *, since we are searching iframe, '//iframe[@id="chatframe"]'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading