0

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()
  • What exactly is output of what you already tried? Why doesnt it click on the button, is there any error or warning? – Lukas Tomek Jul 23 '22 at 19:26
  • Why are you using `ActionChains`? – John Gordon Jul 23 '22 at 19:26
  • "Passthrough is not supported, GL is disabled" but I doubt is has anything to do with it – poopylooper Jul 23 '22 at 19:28
  • I am using ActionChains, because it would give me this error if I don't "selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (1128, 917). Other element would receive the click:
    ...
    (Session info: chrome=103.0.5060.134)"
    – poopylooper Jul 23 '22 at 19:30

2 Answers2

0

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()
Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352