Home > Software engineering >  Selenium (Python) - NoSuchElementException: Message: no such element: Unable to locate element
Selenium (Python) - NoSuchElementException: Message: no such element: Unable to locate element

Time:02-08

I'm trying to make a simple script that goes to the homepage of The Whiskey Exchange, clicks on the following menu item, navigates to the new page and finally grabs a screenshot.

menu item

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

chrome_options = Options()

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.thewhiskyexchange.com/")
driver.implicitly_wait(3)

search_bar = driver.find_element(
    By.XPATH, "/html/body/div[3]/nav/div/div[3]/div/div/div[2]/a[2]")
search_bar.click()
driver.implicitly_wait(3)

driver.save_screenshot('./image.png')
driver.quit()

I've tried multiple approaches to get this to work, mainly by changing the type of finder, but with no luck. I've tried copying both XPATH and Full XPATH (directly from chrome), but that hasn't worked either. Maybe it's something to do with how the site is built, but no idea.

I'm getting the following error message:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/nav/div/div[3]/div/div/div[2]/a[2]"}

First time using selenium, so the struggle is a bit real. Please help.

CodePudding user response:

CSS_SELECTOR

a.header-button.header-button--search.js-header-button--search

XPATH

//a[@title='Search']

You could simply use this css selector since I don't see an xpath like that.

CodePudding user response:

To expand the Scotch Whisky menu you don't need to click on the item, instead you can simply Scotch_Whisky

  •  Tags:  
  • Related