有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Selenium找不到css/clasname选择器

我正在为一个网站编写一个小的测试脚本,作为测试的一部分,程序需要点击Buttons。 我正在使用google chrome驱动程序,并尝试了一些功能,例如: 驾驶员findElement(由.cssSelector()实现) 驾驶员findElements(By.xpath()) 但由于某些原因,程序无法识别定位器

in the image i selected a button to get its id or class (很抱歉,网站是希伯来语的)

主RunnerClass

 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 {

}

步骤定义类

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 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 consumeSteps {

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 pizzahut\\.co\\.il$")
public void i_access_pizzahut_co_il() throws Throwable {
    driver.get("https://pizzahut.co.il/");

}

@When("^I click on sales section button$")
public void i_click_on_sales_section_button() throws Throwable {
  driver.findElement(By.cssSelector("btn-default btn-block ng-binding ng- 
  isolate-scope")).click();

}

@When("^I click on add family non gluten pizza button$")
public void i_click_on_add_family_non_gluten_pizza_button() throws 
 Throwable {


}

@When("^I click on add button$")
public void i_click_on_add_button() throws Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}

@When("^I click on chosen extra$")
public void i_click_on_chosen_extra() throws Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}

@When("^I click on extra for the whole pizza$")
public void i_click_on_extra_for_the_whole_pizza() throws Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}

@When("^I click on choose and next button$")
public void i_click_on_choose_and_next_button() throws Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}

@When("^I click on continue for payment allert button$")
public void i_click_on_continue_for_payment_allert_button() throws 
 Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}

@When("^I click on continue for payment button$")
public void i_click_on_continue_for_payment_button() throws Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}

@Then("^I should successfully be moved to the payment form$")
public void i_should_successfully_be_moved_to_the_payment_form() throws 
 Throwable {
    // Write code here that turns the phrase above into concrete 
 actions

}



 }

特征文件

 Feature: consume product through sales section.

 Scenario: payment form will display after assembling a product.


Given I access pizzahut.co.il
When I click on sales section button
And I click on add family non gluten pizza button 
And I click on add button 
And I click on chosen extra
And I click on extra for the whole pizza
And I click on choose and next button
And I click on continue for payment allert button
And I click on continue for payment button 
Then I should successfully be moved to the payment form

共 (2) 个答案

  1. # 1 楼答案

    希望这能奏效

    //div[@class='home-page-actions_promotion-btn']//a
    
  2. # 2 楼答案

    请尝试以下内容:

    driver.findElement(By.xpath("//*[text()=\"לכל המבצעים\"]")).click();