-1

When i inspect the button I can see this:

<button type="button" class="wds-c-button--primary wds-c-button--small wds-c-button wds-u-hide--nav-width-down" role="button" data-nav-close="" data-testid="application-header-login-link">Log in</button>

I have tried finding the element with these commands but it does not work:

driver.findElement(By.xpath("//button[contains(text(),'Log in')]")).click();
driver.findElement(By.xpath("//button[@type='button' and text()='Log in']")).click();

I get an error in console:

Element could not be scrolled into view

Any ideas on why this is happening? I even added a Thread.sleep(10000) before running these commands.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Steven S
  • 33
  • 3
  • 1
    click() method will first scroll the element into view. Something is preventing that. It's possible that another action is needed first and this button is either hidden, has a size of 0... or has no position at all in the DOM. You might try getting the element's visibility, size and position to see if that sheds any light here. – pcalkins Apr 14 '22 at 16:56

3 Answers3

0

I request you to please add more details. Based on my understanding & knowledge, I will give my suggestions. (I've done a few edits in answer for more understanding)

(Note: I'm writing the code in Java based on the latest versions of all components. So, please refer to the documentation of your version in case.)

Using JavascriptExecutor & Explicit Wait (kindly import the appropriate libraries first):

long seconds = 10;  /* Enter amount of seconds you want to use in wait */
/* 'driver' is a variable of type WebDriver */
/* Define locators and element we want to use here */
String logInBtnXpath = "//button[@type='button' and text()='Log in']";
By by = By.xpath(logInBtnXpath);
WebElement logInBtn = driver.findElement(by);
/* Declaring wait to be able to use explicit wait */
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(seconds));
wait.until(ExpectedConditions.presenceOfElementLocated(by));
/* Bringing element into view */
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);", logInBtn);
/* Wait until element is clickable */
wait.until(ExpectedConditions.elementToBeClickable(logInBtn));
/* Click using javascript if click() method of WebElement doesn't work */
jse.executeScript("arguments[0].click();", loginBtn);
Rahul
  • 139
  • 3
0

There could be some other element overlapping the same position. This can happen if elements don't have a defined position (or have relative positions) and the resolution crunches the elements to overlap.

0

To click on the <button> with text as Log in you need to induce WebDriverWait for the elementToBeClickable() which by default scrolls the element into the view and you can use either of the following locator strategies:

compatible code

  • cssSelector:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.wds-c-button--primary.wds-c-button--small.wds-c-button.wds-u-hide--nav-width-down[data-testid='application-header-login-link']"))).click();
    
  • xpath:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='wds-c-button--primary wds-c-button--small wds-c-button wds-u-hide--nav-width-down' and text()='Log in']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352