首页 > 解决方案 > 无法设法使用 Xpath 单击按钮

问题描述

嗨,我正在尝试在 chrome 浏览器上使用 Xpath 单击按钮,但由于某种原因,该软件没有单击它。我使用 devtools 检查将 Xpath 复制到 findElement 函数。那是网站:https : //mynames.co.il/ 对不起,这是希伯来语...

这张照片显示了按钮,我将按钮标记为蓝色

这是步骤文件:

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();
}

那是跑步者类:

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 {

    
}

标签: javaseleniumxpathcss-selectorswebdriverwait

解决方案


嘿,请使用此 Xpath 单击您用蓝色线条标记的那个按钮

//span[contains(text(),'כניסה')]

推荐阅读