Its pretty self explanatory as to what the script does but to summarize, it opens youtube and types “hello” into the search bar then presses enter. The issue is that it opens youtube but doesn’t type hello or press enter.
heres the code
<code>#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Set up the driver for Chrome
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
# Replace the URL below with the webpage you want to interact with
driver.get('https://youtube.com')
# Give the page some time to load
time.sleep(2)
# Locate the search bar using its name attribute
search_bar = driver.find_element(By.ID('search'))
# Words to type
words = ['hello']
# Type each word and press ENTER
for word in words:
search_bar.clear() # Clear the search bar before typing
search_bar.send_keys(word)
search_bar.send_keys(Keys.ENTER)
time.sleep(3) # Wait a bit between each word
# Close the browser when done
driver.quit()
</code>
<code>#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Set up the driver for Chrome
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
# Replace the URL below with the webpage you want to interact with
driver.get('https://youtube.com')
# Give the page some time to load
time.sleep(2)
# Locate the search bar using its name attribute
search_bar = driver.find_element(By.ID('search'))
# Words to type
words = ['hello']
# Type each word and press ENTER
for word in words:
search_bar.clear() # Clear the search bar before typing
search_bar.send_keys(word)
search_bar.send_keys(Keys.ENTER)
time.sleep(3) # Wait a bit between each word
# Close the browser when done
driver.quit()
</code>
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Set up the driver for Chrome
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
# Replace the URL below with the webpage you want to interact with
driver.get('https://youtube.com')
# Give the page some time to load
time.sleep(2)
# Locate the search bar using its name attribute
search_bar = driver.find_element(By.ID('search'))
# Words to type
words = ['hello']
# Type each word and press ENTER
for word in words:
search_bar.clear() # Clear the search bar before typing
search_bar.send_keys(word)
search_bar.send_keys(Keys.ENTER)
time.sleep(3) # Wait a bit between each word
# Close the browser when done
driver.quit()
I’ve tried switching it to Google.com instead of youtube and i’ve checked the element ID for the search but I think its all correct
New contributor
Haker Modz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.