1

Here is my element on HTML:

<a aria-role="button" href="" class="sc-button-play playButton sc-button sc-button-xlarge" tabindex="0" title="Play" draggable="true">Play</a>

I used Selenium to make the click event:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
buttons = driver.find_elements_by_class_name('playButton')

As you can guess, it doesn't work :)

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Alexgl
  • 79
  • 2
  • 8
  • 1
    Try it with xpath – Sushant Jul 13 '18 at 10:46
  • I saw only that people use the id of the element, so I don't know how to use his class – Alexgl Jul 13 '18 at 10:47
  • 1
    Like @ThatBird said, try it with `xpath`. If you're not familiar with it, use the chrome add-in "xPath Finder". – Lodi Jul 13 '18 at 10:48
  • I don't see any call to the click() method in your code. Once you have buttons, what do you do with it? Also, when you say it doesn't work, what error/exception message do you get. This will give us a clue as to the problem. – Breaks Software Jul 13 '18 at 11:08

4 Answers4

4

Try it with xpath -

driver.find_elements_by_xpath("//a[@class = 'playButton']")

and to find xpath you need to do this -

  • Right click on the element you want and click inspect

  • Inspected element will be highlighted in chrome debugger. Right click on that element and bunch of options will open

  • Click on copy and then click on Copy XPath

And use that xpath in the code above.

Sushant
  • 3,499
  • 3
  • 17
  • 34
1

Try this:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(url)
# find all elements with following xPath (returns a list of elelements)
buttons = driver.find_elements_by_xpath("//a[@class = 'playButton']") # using xPath

or if you want to click on one element use this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get(url)
# wait(at least 10 seconds) for element will be clickable and clcick on it
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class = 'playButton']"))).click();

Here you can find more inforamtion about locating elements.

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
1

If you want to click a link, try

driver.find_element_by_link_text("Play").click()

If link text actually displayed on page as PLAY , try

driver.find_element_by_link_text("PLAY").click()
Andersson
  • 51,635
  • 17
  • 77
  • 129
1

There are answers from @Andersson @AndreiSuvorkov and @ThatBird but seems there are still some more factors left for us to consider as follows:

As you are invoking get(url) and in the very next step trying to invoke click() on an element,

  • Instead of find_elements* you need to use find_element* as follows:

    button = driver.find_element_by_class_name('class_name')
    
  • Before you invoke click you need to induce WebDriverWait for the element to be clickable as follows:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click();
    
  • When you desire to click on a particular element take help of a Locator Strategy which will uniquely identify the WebElement within the DOM Tree. For <a> nodes (i.e. anchor tags) LINK_TEXT and PARTIAL_LINK_TEXT must be the preferred option. Apart from those a much conventional way would be to extensively use the class and id attribute (inabsence of class or id attributes fallback on other attributes) to construct an CssSelector or XPath as follows:

  • LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Play"))).click()
    
  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sc-button-play.playButton.sc-button.sc-button-xlarge[title='Play']"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='sc-button-play playButton sc-button sc-button-xlarge' and @title='Play'][contains(.,'Play')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352