Home > Mobile >  Selenium - how to click on the button
Selenium - how to click on the button

Time:02-02

I am looking for the way to click on the button which is located in html this way:

<button  tabindex="0" type="button"> Zaloguj się<span ></span></button>

I tried few ways to find this with no success.

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


# web credentials
username = "[email protected]"
password = "qwerty"

# initialize the Chrome driver
driver = webdriver.Chrome("chromedriver")

# head to login page
driver.get("https://tzgirls.app/login")

# find username/email field and send the username itself to the input field
driver.find_element("id", "login_email").send_keys(username)

# find password input field and insert password as well
driver.find_element("id", "login_password").send_keys(password)

#import time
#time.sleep(2)

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(),'Zaloguj się')]"))).click()

Could you please advise? Thanks Paulina

CodePudding user response:

To login within the website filling up the E-mail and Password field and clicking on the Zaloguj się element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

driver.get("https://tzgirls.app/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.MuiButton-root.MuiButton-text.MuiButton-textPrimary.MuiButton-sizeMedium.MuiButton-textSizeMedium.MuiButtonBase-root"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login_email"))).send_keys("[email protected]")
driver.find_element(By.CSS_SELECTOR, "input#login_password").send_keys("asdfg")
driver.find_element(By.XPATH, "//button[text()='Zaloguj się']").click()

Browser Snapshot:

Paulina

CodePudding user response:

  1. find_elements_by_xpath is now depricated, as welll as all the rest of find_elements_by* methods.
  2. .css-1rs4rtx is not a class name, this is a CSS Selector style.
  3. findElement is Java style etc.
    Try using this:
driver.find_element(By.CSS_SELECTOR, "button.MuiButton-containedPrimary").click()

Probably it would be better to use WebDriverWait expected_conditions to click the element when it becomes clickable.
If so this can be done as following:

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

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.MuiButton-containedPrimary"))).click()
  • Related