0

hi i'm trying to click on a button using Xpath on chrome browser but from some reason the software does not click on it. i used the devtools inspect in order to copy the Xpath to the findElement function. that's the website: https://mynames.co.il/ i'm sorry that's in hebrew...

this photo shows the button ,i marked the button in blue

that's the steps file:

package stepDefinitions;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.PendingException;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class purchaseDomainSteps {

    
    
WebDriver driver;
    
    
    @Before
    public void setup() throws IOException {
        System.setProperty("webdriver.chrome.driver", Paths.get(System.getProperty("user.dir")).toRealPath() +  "\\src\\test\\java\\drivers\\chromedriver.exe");
        this.driver = new ChromeDriver();
        this.driver.manage().window().maximize();
        this.driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
    }
    
    @After() 
    public void tearDown() {
        this.driver.manage().deleteAllCookies();
        this.driver.quit();
    }
    
    
    
    

@Given("^I access https://mynames\\.co\\.il$")
public void i_access_https_mynames_co_il() throws Throwable {
    driver.get("https://mynames.co.il/");
    throw new PendingException();
}

@When("^I click on Login button\\.$")
public void i_click_on_Login_button() throws Throwable {
    
    
    String path = "/html/body/div[1]/div/div/section[2]/div/div/div[2]/div/div/section/div/div/div[2]/div/div/div/div/div/a/span/span";
    //WebDriverWait wait = new WebDriverWait(driver, 5);
    driver.findElement(By.xpath(path)).click();
    throw new PendingException();
}

that's the runner class:

package runners;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(features = { "src/test/java/featurefiles/" }, glue = {
        "stepDefinitions" }, monochrome = true, tags = {}, 
                plugin = { "pretty", "html:target/cucumber", "json:target/cucumber.json",
                "com.cucumber.listener.ExtentCucumberFormatter:output/report.html" })


public class MainRunner {

    
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

0

Hey pls use this Xpath to click that button which you marked in blue colour line

//span[contains(text(),'כניסה')]
Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

After I've check the website (https://mynames.co.il/):

<div class="elementor-button-wrapper">
   <a href="https://dash.mynames.co.il/login" target="_blank" class="elementor-button-link elementor-button elementor-size-xs" role="button">
      <span class="elementor-button-content-wrapper">
         <span class="elementor-button-text">כניסה</span>
      </span>
   </a>
</div>

I recommend 2 options:

  1. Use the link itself to redirect on login page
String targetPage = driver.findElement(By.xpath("/html/body/div[1]/div/div/section[2]/div/div/div[2]/div/div/section/div/div/div[2]/div/div/div/div/div/a")).getAttribute("href");
driver.navigate().to(targetPage);
  1. (Maybe this is what you want) You click on href or force a href to act like button
driver.findElement(By.xpath("/html/body/div[1]/div/div/section[2]/div/div/div[2]/div/div/section/div/div/div[2]/div/div/div/div/div/a")).click();

The reason that your code is not working is because span that you assume as button, doesn't have any click action meanwhile the click action you hope for is on a href.

// this is just text with style inside span
<span class="elementor-button-text">כניסה</span>
Fahim Bagar
  • 798
  • 7
  • 17
0

Are you getting any Exception ? If yes, what exception is that ? If no, selenium clicking is happening but the application is not acknowledging the click. So try using javascript executor to perform click as below

WebElement ele = driver.findelement(By.xpath("//span[text()='כניסה']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("argument[0].click();",ele); 

Even after using the above code, if it doesn't work, then check if you are referring to the right feature and class files in junit runner class cucumberOptions 'glue' and 'features'

One more important tip : I see absolute xpath in your script which will not work if there is a change in the DOM structure in future, So always go for relative xpath

Partha
  • 107
  • 9
0

As the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.elementor-button-link.elementor-button.elementor-size-xs span.elementor-button-text"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='elementor-button-text' and text()='כניסה']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352