Home > Back-end >  How to click on "hidden" button in code on a page with Selenium?
How to click on "hidden" button in code on a page with Selenium?

Time:04-27

I'm trying to click on the button "Entrar" in this page: https://conta.stone.com.br/login

But neither using these classes:

"sc-AxhUy hclPzu"

"sc-AxhCb jYjZpM"

Nor the XPaths:

"//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span"

"//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span/button"

The code here currently looks like this:

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

option = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=option, executable_path=config.CHROME_PATH)

login = "https://conta.stone.com.br/login"


browser.get(login)
username = browser.find_element(by=By.XPATH, value='//*[@id="username"]')
password = browser.find_element(by=By.XPATH, value='//*[@id="password"]')


username.send_keys(email)
password.send_keys(password)

#browser.find_element_by_xpath('//*[@id="recaptcha-anchor"]/div[1]').click()
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://www.google.com/recaptcha/api2/anchor')]")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='recaptcha-checkbox-border']"))).click()


WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span/button')))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span/button'))).click()

What the hell am I doing wrong currently? I've tried most methods that I could think of to find the element but no dice, if anyone can suggest anything it would be great, everything works fine till this button, even the recaptcha passes most of the time.

I've tried recording the action with chrome and it gave me the following action:

{
    const targetPage = page;
    const element = await waitForSelectors([["aria/Entrar","aria/[role=\"generic\"]"],["#__next > div.clean-page-container__Container-sc-fzay79-0.dGAlqK > form > div.sc-fzoant.MgHsy.card__Card-sc-2xb3kk-0.vMIBG > div.sc-fznLPX.fGbfOF > span > span > button > span"]], targetPage, { timeout, visible: true });
    await scrollIntoViewIfNeeded(element, timeout);
    await element.click({ offset: { x: 25.15625, y: 12.828125} });
}

I've tried running it with the "execute_script" within selenium but as the targetPage wasn't defined in Python it didn't really work.

CodePudding user response:

I found the answer, the problem was being stuck inside the iframe to solve the recaptcha.

Placing a browser.switch_to.default_content() before getting to the button solved it.

CodePudding user response:

(Updated) - Its working for me Please check it :

(Python)

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

option = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=option, executable_path=config.CHROME_PATH)

login = "https://conta.stone.com.br/login"

browser.get(login)
username = browser.find_element(by=By.XPATH, value='//*[@id="username"]')
password = browser.find_element(by=By.XPATH, value='//*[@id="password"]')

username.send_keys(email)
password.send_keys(password)

browser.find_element_by_xpath('(//BUTTON[@shape='regular'])[1]').click()

(JAVA)

WebDriver driver;
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30, 1));
driver.manage().window().maximize();
driver.get("https://conta.stone.com.br/login");
         
By Btn = By.xpath("(//BUTTON[@shape='regular'])[1]");
By Email = By.xpath("//*[@id='username']");
driver.findElement(Email).sendKeys("asdasdasd");
By Pass = By.xpath("//*[@id='password']");
driver.findElement(Pass).sendKeys("eefdsfwewrwer123");

driver.findElement(Btn).click();
  • Related