Selenium – how to click on the button
Question:
I am looking for the way to click on the button which is located in HTML this way:
<button class="MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButtonBase-root css-1rs4rtx" tabindex="0" type="button"> Zaloguj się<span class="MuiTouchRipple-root css-w0pj6f"></span></button>
I tried a few ways to find this without any 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()
How can I do it?
Answers:
find_elements_by_xpath
is now deprecated, as well as all the rest of find_elements_by*
methods.
.css-1rs4rtx
is not a class name; this is a CSS Selector style.
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()
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:
I am looking for the way to click on the button which is located in HTML this way:
<button class="MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButtonBase-root css-1rs4rtx" tabindex="0" type="button"> Zaloguj się<span class="MuiTouchRipple-root css-w0pj6f"></span></button>
I tried a few ways to find this without any 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()
How can I do it?
find_elements_by_xpath
is now deprecated, as well as all the rest offind_elements_by*
methods..css-1rs4rtx
is not a class name; this is a CSS Selector style.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()
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: