Home > database >  Selenium Python - .click() does not seem to be working properly
Selenium Python - .click() does not seem to be working properly

Time:02-20

I am trying to learn Selenium through Python.

I am attempting to create a basic Selenium program where I go on YouTube, search up a video, and click on it to play it.

However, I am having a problem in the line of code that is supposed to find the HTML element of the video to click on it. It is stating, no such element: Unable to locate element: .... However, after further looking into the program, I realized that my code does not ever actually click on the search icon in YouTube to actually enter my search for the video. So I assume that the .click() is not working properly, even though it is not raising an error. I have tried using Keys.RETURN and Keys.ENTER, both gave me the same result. I have also tinkered with the .find_element and trying to see if finding the element through By.CLASS_NAME, Xpaths (Xpath and Full Xpath), or other HTML identifications. None of them succeeded.

Here is my code:

from curses import keyname
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# create driver
driver = webdriver.Chrome()

# get requests to website
driver.get('http://youtube.com')

# search up something in youtube
searchBox = driver.find_element(By.XPATH,'/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div[1]/div[1]/input')
searchBox.send_keys('Genkai')

driver.implicitly_wait(10)

# click search
searchButton = driver.find_element(By.XPATH,'//*[@id="search-icon-legacy"]/yt-icon')
searchButton.click()

driver.implicitly_wait(10)

# click on video
video = driver.find_element(By.XPATH,'/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-video-renderer[2]/div[1]/div/div[1]/div/h3/a')
print(video.text)
video.click()

CodePudding user response:

enter image description here

enter image description here

The code is working in Firefox.

from curses import keyname
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# create driver
driver = webdriver.Firefox()

# get requests to website
driver.get('http://youtube.com')

# search up something in youtube
searchBox = driver.find_element(By.XPATH,'/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div[1]/div[1]/input')
searchBox.send_keys('Genkai')

driver.implicitly_wait(10)

# click search
searchButton = driver.find_element(By.XPATH,'//*[@id="search-icon-legacy"]/yt-icon')
searchButton.click()

driver.implicitly_wait(10)

# click on video
video = driver.find_element(By.XPATH,'/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-video-renderer[2]/div[1]/div/div[1]/div/h3/a')
print(video.text)
video.click()

Please check whether the driver which you are using for the chrome is different.

CodePudding user response:

You are simply missing explicit wait.

Below code I have run on Firefox driver.

Code:

driver = webdriver.Firefox(executable_path = "path to \geckodriver.exe")
driver.maximize_window()

wait = WebDriverWait(driver, 20)

driver.get('https://www.youtube.com')
search_box = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[id='search']")))
search_box.send_keys('Genkai', Keys.RETURN)

video = wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-video-renderer[2]/div[1]/div/div[1]/div/h3/a')))
print(video.get_attribute('innerText'))
video.click()

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Output:

Gravy beats - genkai || 1 hour

Process finished with exit code 0

Therefore this xpath

/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-video-renderer[2]/div[1]/div/div[1]/div/h3/a

can be improved further with

//a[@title='Gravy beats - genkai || 1 hour']

Also, verified on chrome, this code is working fine with cross-browser.

  • Related