Reputation: 246
I am trying to use selenium web driver to open up Google search for Youtube and then click on the Youtube link. (I know I could go straight there but this is a proof of concept) I have played around with different solutions, but I keep having problems locating the Youtube link for me to click on.
Here is what I have:
driver = webdriver.Chrome()
driver.get("https://google.com")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("youtube")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
link = driver.find_element_by_link_text('YouTube')
link.click()
I can't get the HTML to recognize so just inspect it on google to see the HTML for Youtube.
Also answers in python would be appreciated.
Upvotes: 4
Views: 2328
Reputation: 193088
To open up Google search for Youtube and then click on the Youtube link, here is your own working code with some simple tweaks:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome("C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(20)
driver.get("https://google.com")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("youtube")
elem.send_keys(Keys.RETURN)
time.sleep(3)
assert "No results found." not in driver.page_source
driver.find_element_by_xpath('.//*[@id="rso"]/div[1]/div/div/div/div/h3/a').click()
Upvotes: 2