0

HTML Code for the text bar is the following :

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

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]"}

3 Answers3

1

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'})
0

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" class="search_in">

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']
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

find_element_by_class_name() would accept a single classname as an argument. So you can't pass the attribute data-v-0401fc16.

Moreover the attribute data-v-0401fc16 is a dynamic attribute and may change when you would access the application afresh.


Solution

To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.search_in[placeholder='Choose company']"))).send_keys("Sarvesh Sharma")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='search_in' and @placeholder='Choose company']"))).send_keys("Sarvesh Sharma")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352