Alexgl
Alexgl

Reputation: 79

Click on HTML element using Python

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

Upvotes: 1

Views: 6510

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193058

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

Upvotes: 1

Andrei
Andrei

Reputation: 5637

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.

Upvotes: 1

Sushant
Sushant

Reputation: 3669

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.

Upvotes: 4

Andersson
Andersson

Reputation: 52665

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

Upvotes: 1

Related Questions