Home > Back-end >  Error when trying to find an element using SELENIUM
Error when trying to find an element using SELENIUM

Time:02-10

Hopefully someone can help me out here.

My goal is to successfully connect to this website using Python (Selenium Chromedriver) and successfully search for an address in the search bar. Im struggling to manage to find the actual search bar itself using Selenium. The HTML code doesn't have an ID or NAME to distinguish it by, all it has is a class and when i search for the class, i get an error saying it cannot be found.

Hopefully this is an easy fix and im just being silly. Thanks a lot.

P.S First post - please drop some hints on what to change next time :)

CODE

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://creeper.banano.cc/")
search = driver.find_element_by_class_name("ValidatedSearch form-control form-control-lg ")

ERROR CODE

NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".ValidatedSearch form-control form-control-lg "}

Python Code HTML of Website

CodePudding user response:

ValidatedSearch form-control form-control-lg

is what are referred to as multiple class names.

In order to find the element we use css selector.

driver.find_element(By.CSS_SELECTOR,".ValidatedSearch.form-control.form-control-lg")

Also don't use driver.find_element_by since they are depreciated instead import By and use that.

CodePudding user response:

The error here is you have multiple class names in a function only expecting one. ValidatedSearch, form-control, and form-control-lg are different classes, since classes can't have spaces in them. So what you're looking for is this:

search = driver.find_element_by_class_name("ValidatedSearch")

This targets the first tag in the ValidatedSearch class that it finds.

CodePudding user response:

I was able to get it to work with

search = driver.find_element_by_class_name("ValidatedSearch")
search.send_keys("Hi My Name is Slicka Slicka Slim Shady")

I used one class. I would say Selenium iirc only requires a single class. Also Im not primarily a webdesigner but these frameworks that are all over today are pretty much add the additionals are annoying. When I create Selenium scripts I use xpaths more and more more. You may want to look into that

  • Related