java - Selenium 找不到 css/classname 选择器

标签 java selenium-webdriver cucumber google-chrome-devtools

我正在为网站编写一个小型测试脚本,作为测试的一部分,程序需要单击按钮。 我正在使用谷歌浏览器驱动程序,我尝试了一些功能,例如: driver.findElement(By.cssSelector()) driver.findElements(By.xpath()) 但由于某种原因,程序无法识别定位器。

in the image i selected a button to get its id or 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 {

}

步骤定义类

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

最佳答案

请尝试以下:

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

关于java - Selenium 找不到 css/classname 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418333/

相关文章:

selenium-webdriver - 设计可重复使用的步骤 Specflow - 这种方法正确吗

ruby - 我如何在 capybara 中单击此按钮(使用 'ID' 或 'Xpath' 选择器)

java - Selenium,Java - 无法定位元素

java - cucumber 在时间后逐步停止执行

java - 第一年程序员需要有关 java 中空指针异常的帮助

java - 如何读取用于 OpenSAML 的私钥?

selenium-webdriver - 错误 : ENOENT: no such file or directory, scandir '/Documents/android-sdk-linux/build-tools' 错误( native )

Java - 涉及位的基本编程

java - 创建 Java 充电电池测试器

python - 通过调用页面的 PageObject 导航到该页面