Selenium | css-selector not found

Hi, I’m trying stuf with selenium.

For some reason the selector I get isn’t found.

code:


#Importing libraries
from selenium import webdriver 
import pandas as pd 
from selenium.webdriver.common.keys import Keys
import time
# creating instance for web driver
driver = webdriver.Chrome()

driver.get("https://soundcloud.com/discover")

print(  driver.title)

inputElement = driver.find_element_by_css_selector("#app > header > div > div.header__middle > div > form > input")

inputElement.send_keys("dog")

inputElementClick = driver.find_element_by_css_selector("#app > header > div > div.header__middle > div > form > button")

inputElementClick.send_keys(Keys.RETURN)

links = []

linkElement = driver.find_element_by_css_selector("#content > div > div > div.l-main > div > div > div > ul > li:nth-child(2) > div > div > div > div.sound__content > div.sound__header > div > div > div.soundTitle__usernameTitleContainer > a > span")
print (linkElement)

console:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#content > div > div > div.l-main > div > div > div > ul > li:nth-child(2) > div > div > div > div.sound__content > div.sound__header > div > div > div.soundTitle__usernameTitleContainer > a > span"}

(I’m also figuring out how to loop this and put all the results in a list) Any help is gladly appreciated! :slight_smile:

I was experimenting with selenium too a VERY little bit, so this might be an answer that is not the solution, but since it seems missing in your code:

Is your executable driver path defined? (like driver_path = <wherever the executable is on your computer.>
Maybe it cannot be found for whatever reason, so it might be worth to check if it can be found of define it to be sure.

The driver path is working, sorry I removed this because of potential sensitive info.

Everything before the last line works. Only the last css selector doesn’t. Very strange.

This was the solution to my problem!


driver.get("https://soundcloud.com/search?q=dog")

time.sleep(5)
elem = driver.find_element_by_tag_name("body")
no_of_pagedowns = 20
while no_of_pagedowns:
          elem.send_keys(Keys.PAGE_DOWN)
          time.sleep(1)
          no_of_pagedowns-=1

driver.implicitly_wait(100)

for a in driver.find_elements_by_class_name("sound__coverArt"):

        print(a.get_attribute('href'))

Afterwards, I would consider a timing issue. Has the DOM fully loaded before searching for this element? Selenium comes with different waits (implicit, explicit) which you can use to wait for certain elements to be loaded. A quick (and usually bad) way of testing this would just be to add:

time.sleep(5)

After you get open the browser, and before the element you’re looking for, to give it time to load.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.