ccva
ccva

Reputation: 23

Unable to click button using Selenium JavaScript

I am not able to click the login button, my code is correct and there is no iframe or window:

const { Builder, By, Key, until } = require('selenium-webdriver');
const { expect } = require('chai');

describe('SignupTest', () => {
    const driver = new Builder().forBrowser('chrome').build();

    it('signup with valid email and valid password', async () => {
        await driver.get('https://ca.letgo.com/en');
        const loginButton = driver.findElement(By.xpath("//button[contains(.,'Log In')]"));
        driver.execute("arguments[0].click()",loginButton)
        //await driver.findElement(By.xpath("//button[contains(.,'Log In')]")).click();
        //name=email
        //name=password
        //name=name
        //xpath=//span[contains(.,'Sign Up')]
        //await driver.sleep(20000);
    });

Upvotes: 2

Views: 254

Answers (2)

udhaya kumar
udhaya kumar

Reputation: 136

If your goal is using javascript for running the selenium means. i suggest you using nightwatch js which is uses the selenium and node js.

In your case you can do the below.

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

or

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

You can also check the visibity before click using isDisplay() . If it prints true then perform the click action.

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193058

The element with text as Log In is actually within a child <span> tag (of the <button> tag) and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    button[data-test='login'][data-testid='login']>span
    
  • Using XPATH:

    //span[text()='Log In']
    

Note: You need to induce WebDriverWait while you attempt to invoke click() on the element with text as Log In.

Upvotes: 0

Related Questions