简体   繁体   English

Selenium 无法定位元素

[英]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/ .我正在这个网站上工作, 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尝试复制“完整 xpath”并将其放置在 xpath

search_bar = driver.find_element_by_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 :要将字符序列发送到搜索字段,您需要为element_to_be_clickable()引入WebDriverWait ,并且可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用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 :使用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: 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.您在脚本中使用的 xpath 绝对是 xpath,执行脚本时它发生变化的可能性非常高。 You should use relative and stable xpath.你应该使用相对稳定的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-搜索这个 xpath-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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