Selenium (Python) - NoSuchElementException: 消息: 没有这样的元素: 无法定位元素
Selenium (Python) - NoSuchElementException: Message: no such element: Unable to locate element
我正在尝试制作一个简单的脚本,该脚本可以转到 The Whiskey Exchange 的主页,单击以下菜单项,导航到新页面,最后抓取屏幕截图。
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()
我尝试了多种方法来让它工作,主要是通过更改取景器的类型,但没有成功。我试过复制 XPATH 和 Full XPATH(直接从 chrome),但这也没有用。可能与网站的构建方式有关,但不知道。
我收到以下错误消息:
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]"}
第一次使用selenium,所以纠结有点真实。请帮忙。
CSS_SELECTOR
a.header-button.header-button--search.js-header-button--search
XPATH
//a[@title='Search']
您可以简单地使用这个 css 选择器,因为我没有看到这样的 xpath。
要展开 苏格兰威士忌 菜单,您无需单击该项目,只需 and grab a screenshot of the webpage using either of the following :
使用CSS_SELECTOR:
driver.get('https://www.thewhiskyexchange.com/')
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[title='Scotch Whisky']")))).perform()
driver.save_screenshot('./Scotch_Whisky.png')
driver.quit()
使用 XPATH:
driver.get('https://www.thewhiskyexchange.com/')
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@title='Scotch Whisky']")))).perform()
driver.save_screenshot('./Scotch_Whisky.png')
driver.quit()
注意:您必须添加以下导入:
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
截图:
我正在尝试制作一个简单的脚本,该脚本可以转到 The Whiskey Exchange 的主页,单击以下菜单项,导航到新页面,最后抓取屏幕截图。
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()
我尝试了多种方法来让它工作,主要是通过更改取景器的类型,但没有成功。我试过复制 XPATH 和 Full XPATH(直接从 chrome),但这也没有用。可能与网站的构建方式有关,但不知道。
我收到以下错误消息:
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]"}
第一次使用selenium,所以纠结有点真实。请帮忙。
CSS_SELECTOR
a.header-button.header-button--search.js-header-button--search
XPATH
//a[@title='Search']
您可以简单地使用这个 css 选择器,因为我没有看到这样的 xpath。
要展开 苏格兰威士忌 菜单,您无需单击该项目,只需
使用CSS_SELECTOR:
driver.get('https://www.thewhiskyexchange.com/') ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[title='Scotch Whisky']")))).perform() driver.save_screenshot('./Scotch_Whisky.png') driver.quit()
使用 XPATH:
driver.get('https://www.thewhiskyexchange.com/') ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@title='Scotch Whisky']")))).perform() driver.save_screenshot('./Scotch_Whisky.png') driver.quit()
注意:您必须添加以下导入:
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
截图: