I'm trying to click a button on a website with Selenium in python, and the button clicking works fine, I can see the button being clicked but the code stops running at that point and when I check the website in my normal browser I can tell that the button hasn't been clicked. More specifically, I'm trying to click a start button for an aternos.org server, and it clicks fine, but the actual starting of the server doesn't go through for some reason.
My code for the start button clicking:
start = driver.find_element(By.ID, 'start')
status = driver.find_element(By.CLASS_NAME, 'statuslabel-label').text
print(status)
if status == 'Offline':
start.click()
print('Starting server!')
Ideally, to locate and click any clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using ID:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "start"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#start"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='start']"))).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
Related
I'm trying to automate a login with a user and password on a website, which is this one :
https://www.esselunga.it/area-utenti/applicationCheck?appName=esselungaEcommerce&daru=https%3A%2F%2Fwww.esselungaacasa.it%3A443%2Fecommerce%2Fnav%2Fauth%2Fsupermercato%2Fhome.html%3F&loginType=light
I managed to insert the user and the password trough Selenium, but I can't click on the "Accedi" button, which is the Italian word for "Sign In".
HTML:
<div>
<button value="Accedi" type="submit">Accedi</button>
</div>
I tried the following python code:
1st try:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[2]/div[2]/div[1]/form[1]/div/button"))).click()
2nd try:
button = driver.find_element(by=By.CSS_SELECTOR, value="Accedi")
3rd try:
button = driver.find_element(by=By.LINK_TEXT, value="Accedi")
button.click()
But without success.
Can you please help me with this personal project?
You can try:
# Needed libs
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
import time
#Open the browser
driver = webdriver.Edge()
url = 'https://www.esselunga.it/area-utenti/applicationCheck?appName=esselungaEcommerce&daru=https%3A%2F%2Fwww.esselungaacasa.it%3A443%2Fecommerce%2Fnav%2Fauth%2Fsupermercato%2Fhome.html%3F&loginType=light'
driver.get(url)
user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, f"gw_username")))
user.send_keys('User#gmai.com')
user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, f"gw_password")))
user.send_keys('password')
login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//button[#value='Accedi']")))
login_button.click()
time.sleep(10)
But I see a big problem here...and the problem is that you have a captcha. With the hints I give you will solve the problem of clicking into the element, but in the moment you need to pass the captcha...that will be a different topic because captchas are created for avoiding bots (Selenium is a bot managing your browser).
For the problem with the captcha you can check this other answer: Captctha
I hope it helps!
To click on Accedi first you have to click on the reCAPTCHA checkbox which is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the reCAPTCHA element to be clickable.
Induce WebDriverWait for Accedi element to be clickable.
You can use the following locator strategies:
Using CSS_SELECTOR:
driver.get('https://www.esselunga.it/area-utenti/applicationCheck?appName=esselungaEcommerce&daru=https%3A%2F%2Fwww.esselungaacasa.it%3A443%2Fecommerce%2Fnav%2Fauth%2Fsupermercato%2Fhome.html%3F&loginType=light')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#accettaTuttiCookie"))).click()
driver.find_element(By.CSS_SELECTOR, "input#gw_username").send_keys("LeonardoVarè#stackoverflow.com")
driver.find_element(By.CSS_SELECTOR, "input#gw_password").send_keys("LeonardoVarè")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='reCAPTCHA']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[value='Accedi']"))).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
I want to click on a popup window but after trying several ways, I obtain a "TimeoutException" error.
This is the code that I'm trying:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='modal-footer modal-footer-gallit-postula' and #id='modal_comentarios']"))).click()
And I attach an image of the html
For further information, the popup is very similar to the one appearing in the url below after clicking on the green button that says "Postular" (to get the exact popup it's necessary to be logged in). https://trabajo.gallito.com.uy/anuncio/vendedor-automotriz-qm995
The desired element is within a Modal Dialog Box so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.modal-footer.modal-footer-gallit-postula > button.btn.btn-primary.btn-color-postula"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary btn-color-postula' and text()='Aceptar']"))).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
You can use time.sleep() to handle this. For context:
from selenium import...
import time
driver.get(url)
butn = driver.find_element(by=By.XPATH, value='//*
[#id="postular"]').click()
time.sleep(5)
driver.find_element(by=By.XPATH, value='____').click()
-I don't have the credentials to login, but this is how i handle modal alerts
I am trying to create a web application that automately republishes on a site for private sales.
Login and everything went well, but if I try to find and click the republish button it just wont work.
I've tried it with every locator, but the problem is that every button got an uniqe ID.
Example:
<button name="republish" type="button" data-testid="5484xxxxx-republish-button" class="Button__ButtonContainer-sc-3uxxxx-0 hxXxxX">Republish</button>
The last I've tried:
buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Republish')]")
for btn in buttons:
btn.click()
But it also didnt work, same with By.NAME, BY.TAG_NAME
The <button> element looks to be dynamically generated so to click elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid$='republish-button'][name='republish']"))))
Using XPATH:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#class, 'Button__ButtonContainer') and contains(#data-testid, 'republish-button')][#name='republish' and text()='Republish']"))))
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
My selenium python script can't click the button with either click() or `driver.execute_script("arguments[0].click();", continue_). It worked for previous buttons but this particular button is only detected but can't be clicked.
Here is the code:
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Continue')]")))
continue_ = driver.find_element(
By.XPATH, "//button[contains(text(),'Continue')]")
driver.execute_script("arguments[0].click();", continue_)
Update:
The answers didn't work for me. I tried adding prints to see where it stops.
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Continue')]")))
print("Presence Located")
continue_ = driver.find_element(
By.XPATH, "//button[contains(text(),'Continue')]")
print("Continue Button Found")
driver.execute_script("arguments[0].click();", continue_)
print("Continue Button Clicked")
My console displays all prints till the "Continue Button Clicked" but is still not clicking the Continue so my bot can't progress through it's script. I don't know if it's any help but can't think of anything else.
Update
Tried using is_displayed() on the continue button it returned True.
The <button> element have the innerText spanned over multiple lines.
Solution
To click on the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH and starts-with():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and starts-with(., 'Continue')]"))).click()
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and contains(., 'Continue')]"))).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
I can't seem to find the right path for the button marked above. Any help on how to simulate a click on this button?
This is the website, and linked under is a greater part of the source code.
To click on the element with text as Lukk you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ytcp-button#close-button div[label='Lukk']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytcp-button[#id='close-button']//div[#label='Lukk' and text()='Lukk']"))).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
I would say to first wait for the popup to open. Explicit wait can do:
Here is a sample code:
https://seleniumbyexamples.github.io/waitvisibility
Your selector would be paper-dialog#dialog
Once the popup is visible you can now wait for the button.
You can use wait to be clickable:
https://seleniumbyexamples.github.io/waitclickable
The id would be close-button
You can also try other locators:
https://seleniumbyexamples.github.io/locator