Home > Back-end >  Using selenium (Python) webdriver is unable to locate the text field. Throws up error for finding el
Using selenium (Python) webdriver is unable to locate the text field. Throws up error for finding el

Time:05-05

HTML Code for the text bar is the following :

<input data-v-0401fc16="" type="text" placeholder="Choose company" >

CSS for the bar is as follows, so I also tried to put in the [data-v-0401fc16] in order to get the element by class name.

.search_in[data-v-0401fc16] {
    width: 100vw;
    height: 34px;
    background: none;
    border: none;
    outline: none;
    color: #003a5d;
    font-size: 18px;
    cursor: pointer;
}

How do I get send keys to the bar, I am unable to do so using the following :

search = driver.find_element_by_class_name("search_in[data-v-0401fc16]")
search.send_keys("123") 

The error I get using this method is as follows. I am not able to get the textbar element and unable to input text in it.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".search_in[data-v-0401fc16]"}

CodePudding user response:

You can use BeautifulSoup to find elements in a page and Selenium only to get the page content.

from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.service import Service

try:
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
except WebDriverException as e:
    print(f"{type(e).__name__} : {e}")
    return 1

driver.get(URL)

content = driver.page_source
soup = BeautifulSoup(content, features="html.parser")
matches = soup.findAll('input', attrs={'class': 'search_in'})

CodePudding user response:

You are using the below line of code to find a web element:

search = driver.find_element_by_class_name("search_in[data-v-0401fc16]")

specifically you are using a class_name with this value search_in[data-v-0401fc16]

Based on the html that you've shared,

<input data-v-0401fc16="" type="text" placeholder="Choose company" >

Here

input -> is tag name

data-v-0401fc16 -> attribute name that has value as ""

type -> attribute name that has value as text

placeholder -> attribute name that has value as Choose company

class -> attribute name that has value as search_in

class name is just search_in not search_in[data-v-0401fc16] that's the reason you are getting selenium.common.exceptions.NoSuchElementException:

Solution:

Change class_name to css_selector

search = driver.find_element_by_css_selector("input.search_in[data-v-0401fc16]")
search.send_keys("123") 

Note that . is to represent class, and basic CSS selector follows the below syntax:

tag_name[attr_name = 'attr_value']
  • Related