Selenium не может найти селекторы css / clasname - PullRequest
0 голосов
/ 29 января 2019

Я пишу небольшой тестовый скрипт для веб-сайта, и в рамках теста программе нужно нажать на кнопки.Я использую драйвер Google Chrome, и я попробовал несколько функций, таких как: driver.findElement (By.cssSelector ()) driver.findElements (By.xpath ()), но программа по какой-то причине не распознает локатор.

на изображении я выбрал кнопку, чтобы получить ее идентификатор или класс (извините, сайт на иврите).

Main 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 ]

0 голосов
/ 29 января 2019

Надеюсь, это сработает

//div[@class='home-page-actions_promotion-btn']//a
0 голосов
/ 29 января 2019

Пожалуйста, попробуйте ниже:

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