0

I'm trying to make a bot which adds an item to your basket and checks out. I have a problem clicking on the item using Selenium in Python 3.6.5. I want to be able to click on an item based on its alt attribute of the img tag, so "Dek946uiqbq" here.

<img src="//assets.supremenewyork.com/157783/vi/dek946uiQBQ.jpg" alt="Dek946uiqbq" width="81" height="81">

So far I have done this, but it doesn't work:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('https://www.supremenewyork.com/shop/all')
linkElem = browser.find_element_by_link_text('Dek946uiqbq')
linkElem.click()

Am I using the wrong method?

Guy
  • 46,488
  • 10
  • 44
  • 88
aboruchovas
  • 45
  • 2
  • 5

1 Answers1

4

Dek946uiqbq is not text, find_element_by_link_text can't detect it. Text looks like this in the html

<img src="..." alt="...">This is text</img>

To locate element by attribute you can use css_selector

linkElem = browser.find_element_by_css_selector('[alt="Dek946uiqbq"]')

Or xpath

linkElem = browser.find_element_by_xpath('//img[@alt="Dek946uiqbq"]')
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thanks! It's working now, for the next stage I want to add it to the basket - I've tried browser.find_element_by_name('commit') but it gives me a NoSuchElementException, when the HTML is this: – aboruchovas Oct 18 '18 at 16:02
  • @aboruchovas Try adding [explicit wait](https://selenium-python.readthedocs.io/waits.html#explicit-waits) – Guy Oct 18 '18 at 16:53