I am very new to selenium, and I’m trying to produce a review scraping tool for tripadvisor. I need to enter some text in the search bar, but I can’t get it to work.
This is the page:
https://www.tripadvisor.co.uk/
Here is the html:
html
I tried searching by class, but I get a no such element exception error:
Traceback (most recent call last):
File "C:\Users\raefm\AppData\Roaming\JetBrains\PyCharmCE2021.1\scratches\scraper.py", line 16, in <module>
search = driver.find_element("class name", "qjfqs _G B- z _J Cj R0")
File "C:\Users\raefm\PycharmProjects\scraper\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 740, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
File "C:\Users\raefm\PycharmProjects\scraper\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "C:\Users\raefm\PycharmProjects\scraper\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".qjfqs _G B- z _J Cj R0"}
(Session info: chrome=114.0.5735.199); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
And this is my current code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.tripadvisor.co.uk")
accept = driver.find_element("id", "onetrust-accept-btn-handler")
accept.click()
search = driver.find_element("class name", "qjfqs _G B- z _J Cj R0")
search.send_keys("xyz")
search.send_keys(Keys.RETURN)
time.sleep(100)
Any input would be greatly appreciated!
>Solution :
Root cause of the issue: There are 2 web elements with the ClassName = qjfqs _G B- z _J Cj R0. Also you need to apply waits for selenium to effectively locate the element. These were the reasons for NoSuchElement exception.
Try the below working code:
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.tripadvisor.co.uk/")
driver.maximize_window()
wait = WebDriverWait(driver, 30)
# Accept cookies
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
search = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='qjfqs _G B- z _J Cj R0' and @placeholder='Where to?']")))
search.send_keys("xyz")
search.send_keys(Keys.RETURN)
time.sleep(50)
Result:

