-1

Unable to interact with href link.

Code trials:

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()

Code and Error snapshot:

enter image description here

HTML:

<a href="/cart" class="cart-heading">Cart</a>

HTML Snapshot:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alan Fierro
  • 9
  • 1
  • 4
  • 3
    Please add code itself to your question, not screenshots – Sergey Pugach Dec 29 '18 at 05:04
  • When i copied pasted my code a problem would occur and it woudnt let me post it – Alan Fierro Dec 29 '18 at 05:21
  • It seems your element is hidden. can you post the HTML code rather than snap – NarendraR Dec 29 '18 at 06:01
  • 1
    Welcome to Stack Overflow! As a new user, you probably should read the [tour] to familiarize yourself with the site. There was [formatting help available](https://stackoverflow.com/help/formatting) while you entered your question; please read it now and [edit] your post. If your unspecified ("a") problem was that your post seems to consist largely of *code*, well, now it largely consists of *images*, which is not an improvement. That message is to encourage you to describe in words what the problem is and what you tried to fix it. – Jongware Dec 29 '18 at 08:32

2 Answers2

1

This error message...

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element {"method":"link text","selector":"Cart"}

...implies that the ChromeDriver was unable to locate the desired element as per the line:

GotoCart = browser.find_element_by_link_text("Cart").click()

Solution

You need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • Using LINK_TEXT:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Cart"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "section#header a.cart-heading[href='/cart']"))).click()
    
  • Using XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[@id='header']//a[@class='cart-heading' and @href='/cart']"))).click()
    
  • 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
    

PS: You can find a detailed discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

The error is at the bottom of the stack trace, it can't find the element from the link text that you gave it. It's probably the same problem that this person had where the python was going too fast and the page hadn't fully loaded: How to use find_element_by_link_text() properly to not raise NoSuchElementException?

So just add browser.implicitly_wait(10) right after the line where you set browser.

CornSmith
  • 1,957
  • 1
  • 19
  • 35