NoSuchElementException: Selenium unable to locate search bar regardless of which selector I use (xpath, css_selector, fullXpath etc..)

Viewed 73

I'm trying to automate search procedure on facebook. I used the following code

search = driver.find_element_by_xpath('//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input')
search.send_keys('foo') # search for 'foo'
submit = driver.find_element_by_css_selector('#jsc_c_11 > div > a > div > div.hpfvmrgz.g5gj957u.buofh1pr.rj1gh0hx.o8rfisnq > span > span')
submit.click()

Note that I had attempted the same procedure beforehand using

WebDriverWait(f.browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input'))).send_keys("foo")

To no avail. I keep getting the same error

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input"}

with a TimeoutException for the last entry.

I have managed to automate login using selenium but these NoSuchElementExceptions occur so frequently, I'm starting to wonder if there's a problem with my device

Note : the following code works just fine for my coworkers

1 Answers

If it is the facebook Search Facebook which is located on the left corner top side, you can use the belwo css_selector :

input[name='global_typeahead']

code :

wait = WebDriverWait(driver, 10)
search = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='global_typeahead']")))
search.send_keys('foo')

Imports :

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

PS :- Those css or xpath are not looking reliable, they may work or may not work, always try to write your own locators using axes.

Related