1

I'm trying to locate an element using python selenium, and have the html below:

<input class="form-control" type="text" placeholder="University Search">

I couldn't locate where to type what I want to type.

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path=r"D:\Python\Lib\site-packages\selenium\chromedriver.exe")
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')

#<input class="form-control" type="text" placeholder="University Search">

text_area = driver.find_element_by_name('University Search')
text_area.send_keys("oxford university")
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Dartagnan
  • 21
  • 6

4 Answers4

1

You are attempting to use find_element_by_name and yet this element has no name attribute defined. You need to look for the element with the specific placeholder attribute you are interested in - you can use find_element_by_xpath for this:

text_area = driver.find_element_by_xpath("//input[@placeholder='University Search']")

Also aside: When I open my browser, I don't see an element with "University Search" in the placeholder, only a search bar with "Site Search" -- but this might be a regional and/or browser difference.

costaparas
  • 4,683
  • 11
  • 14
  • 25
  • This give an error: "selenium.common.exceptions.Element Not Interactable Exception: Message: element not interactable" – Dartagnan Dec 19 '20 at 01:13
  • Most likely there's a popup hiding it... Standard solution is to use `ActionChains`, try https://stackoverflow.com/questions/44119081/how-do-you-fix-the-element-not-interactable-exception – costaparas Dec 19 '20 at 01:18
1

Make sure you wait for the page to load using webdriver waits,click the popup and then proceed to target the element to send keys to.

driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
driver.maximize_window()
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK, I agree']"))).click()
text_area=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR ,"td.uni-search.uni.sorting_disabled > div > input")))
text_area.send_keys("oxford university")

Imports

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

Element to target

<input class="form-control" type="text" placeholder="University Search">
Arundeep Chohan
  • 6,219
  • 4
  • 6
  • 22
1

To send a character sequence to the element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("input.form-control[placeholder='University search']").send_keys("oxford university")
    
  • Using xpath:

    driver.find_element_by_xpath("//input[@class='form-control' and @placeholder='University search']").send_keys("oxford university")
    

Ideally, 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:

    driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[placeholder='University search']"))).send_keys("oxford university")
    
  • Using XPATH:

    driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control' and @placeholder='University search']"))).send_keys("oxford university")
    
  • 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
    
  • Browser Snapshot:

oxford


References

You can find a couple of relevant discussions on NoSuchElementException in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

Very close, try the XPath when all else fails:

text_area = driver.find_element_by_xpath("//*[@id='qs-rankings']/thead/tr[3]/td[2]/div/input")

You can copy the full/relative XPath to clipboard if you're inspecting the webpage's html.

Problemo
  • 1
  • 1
  • What's the logic behind this code? Especially "[@id='qs-rankings']/thead/tr[3]/td[2]/div/input" part. – Dartagnan Dec 19 '20 at 01:09
  • XPath is a query language used to navigate XML-like structures. By following the 'path' that leads to the `input` element starting from either the root element of the webpage or a relative one (the middle of the DOM structure), you can locate specific elements using this method. In this particular instance, you are looking at a relative XPath that begins at the `table` element with the id `qs-rankings`. – Problemo Dec 19 '20 at 01:33