How do I use Selenium to click this big button?

Question:

I’ve tried using the elements linktext, value and xpath. I cant seem to make it click on the button with anything. What am I doing wrong?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import time

PATH = "C:/Users/yongs/Downloads/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://ttsfree.com/")

textbox = driver.find_element("id", "input_text")
textbox.send_keys("Text to convert")

driver.implicitly_wait(5)


button_xpath = "/html/body/section[2]/div[2]/form/div[2]/div[2]/a"
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, button_xpath)))

actions = ActionChains(driver)
actions.click(button)
actions.perform()
Asked By: poopylooper

||

Answers:

EDITED to include download solution as well

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.relative_locator import locate_with
import time as t


chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
# chrome_options.add_argument("--headless")


webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

url = 'https://ttsfree.com/'

browser.get(url)


WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button/span[text()='AGREE']"))).click()
textbox = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "input_text")))

textbox.send_keys("Text to convert")
button = WebDriverWait(browser, 2000).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Convert Now']")))
button.click()
print('clicked!')
t.sleep(10)

browser.execute_script('document.getElementsByClassName("label_process text-left")[0].scrollIntoView();')

dl_button = WebDriverWait(browser, 2000).until(EC.element_to_be_clickable((By.CLASS_NAME, "fa-download")))
dl_button.click()
t.sleep(10)
browser.quit()

To click on the element Convert Now you need to induce WebDriverWait for the element_to_be_clickable() which automatically scrolls the element within view and you can use the following locator strategy:

  • Using LINK_TEXT:

    driver.execute("get", {'url': 'https://ttsfree.com/'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea#input_text"))).send_keys("Hello")  
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Convert Now"))).click()
    
  • Using CSS_SELECTOR:

    driver.execute("get", {'url': 'https://ttsfree.com/'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea#input_text"))).send_keys("Hello")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.convert-now"))).click()
    
  • Using XPATH:

    driver.execute("get", {'url': 'https://ttsfree.com/'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea#input_text"))).send_keys("Hello")  
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Convert Now']"))).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
    
Answered By: undetected Selenium
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.