简体   繁体   中英

Selenium unable to locate the element

I keep getting the error that says:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div/div/div[2]/div/div/div/div[2]/div/div/form/input"}

I am working on this website, https://new.igashop.com.au/ . What I am trying to do is to extract the city I want from the google sheet and type it into the search bar so that I can access the website for that particular city. I cannot find what is wrong with my code. Could anyone give me any advice? Thanks

driver = get_webdriver()
driver.delete_all_cookies()
driver.implicitly_wait(10)
driver.get("https://new.igashop.com.au/")
city = product_sheet.col_values(2)[index]
search_bar = driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/div/div/div/div[2]/div/div/form/input')
search_bar.send_keys(city)
search_bar.send_keys(Keys.RETURN)

try copying 'full xpath' and place it instead of xpath

search_bar = driver.find_element_by_xpath('

To send a character sequence to the search field 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://new.igashop.com.au/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='close modal'] > span[data-testid='icon-span-testId']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-testid='desktop-searchInputBox']"))).send_keys("Japan" + Keys.RETURN)
  • Using XPATH :

     driver.get("https://new.igashop.com.au/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='close modal']/span[@data-testid='icon-span-testId']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-testid='desktop-searchInputBox']"))).send_keys("Japan" + Keys.RETURN)
  • 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:

igashop_日本

I am assuming, you do not have problem in getting the city name correctly. If that's working fine, then below code is able to type "city name" inside the search field and then click on it.

Example cityName : "Japan"

driver.find_element(By.ID, "desktop-searchInputBox").send_keys("Japan")
driver.find_element(By.CSS_SELECTOR, "#desktop-searchInputButton svg").click()

Output:

在此处输入图像描述

The xpath which you are using in your script that is absolute xpath and chances are very high that it get change when you execute script. You should use relative and stable xpath. One more thing I noticed that when you are opening this website a popup used to open that you will have to close before accessing the input search box.

For search this xpath-

//input[@id='desktop-searchInputBox']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM