selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法使用 Selenium 和 Python 定位元素
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element using Selenium and Python
无法与 href 交互 link。
代码试验:
browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get(URL)
webbrowser.open(URL)
#if Size == 'Large':
ClickS =browser.find_element_by_id('product-select').click()
SizeS = browser.find_element_by_xpath("//option[@value='12218866696317']").click()
#Send to cart
AddtoCart = browser.find_element_by_css_selector("input[type='submit']").click()
GotoCart = browser.find_element_by_partial_link_text("Cart").click()
代码和错误快照:
HTML:
<a href="/cart" class="cart-heading">Cart</a>
HTML快照:
错误出现在堆栈跟踪的底部,无法从您提供的 link 文本中找到元素。 python 速度太快而页面未完全加载的问题可能与此人遇到的问题相同:How to use find_element_by_link_text() properly to not raise NoSuchElementException?
所以只需在设置 browser
的行之后添加 browser.implicitly_wait(10)
。
这个错误信息...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element {"method":"link text","selector":"Cart"}
...表示 ChromeDriver 无法根据以下行找到所需的元素:
GotoCart = browser.find_element_by_link_text("Cart").click()
解决方案
您需要引入 WebDriverWait 以使所需的 元素可点击,您可以使用以下任一解决方案:
使用LINK_TEXT
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Cart"))).click()
使用CSS_SELECTOR
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "section#header a.cart-heading[href='/cart']"))).click()
使用XPATH
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[@id='header']//a[@class='cart-heading' and @href='/cart']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
PS:你可以在
中找到详细的讨论
无法与 href 交互 link。
代码试验:
browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get(URL)
webbrowser.open(URL)
#if Size == 'Large':
ClickS =browser.find_element_by_id('product-select').click()
SizeS = browser.find_element_by_xpath("//option[@value='12218866696317']").click()
#Send to cart
AddtoCart = browser.find_element_by_css_selector("input[type='submit']").click()
GotoCart = browser.find_element_by_partial_link_text("Cart").click()
代码和错误快照:
HTML:
<a href="/cart" class="cart-heading">Cart</a>
HTML快照:
错误出现在堆栈跟踪的底部,无法从您提供的 link 文本中找到元素。 python 速度太快而页面未完全加载的问题可能与此人遇到的问题相同:How to use find_element_by_link_text() properly to not raise NoSuchElementException?
所以只需在设置 browser
的行之后添加 browser.implicitly_wait(10)
。
这个错误信息...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element {"method":"link text","selector":"Cart"}
...表示 ChromeDriver 无法根据以下行找到所需的元素:
GotoCart = browser.find_element_by_link_text("Cart").click()
解决方案
您需要引入 WebDriverWait 以使所需的 元素可点击,您可以使用以下任一解决方案:
使用
LINK_TEXT
:WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Cart"))).click()
使用
CSS_SELECTOR
:WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "section#header a.cart-heading[href='/cart']"))).click()
使用
XPATH
:WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[@id='header']//a[@class='cart-heading' and @href='/cart']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
PS:你可以在