【问题标题】:Selenium cannot find css/clasname selectorsSelenium 找不到 css/类名选择器
【发布时间】:2019-01-29 18:51:58
【问题描述】:

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

in the image i selected a button to get its id or class (对不起,该网站是希伯来语的)。

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

【问题讨论】:

  • 该网站可以使用其页面脚本动态添加内容(许多现代网站都这样做)。尝试以 100 毫秒的间隔定期重新检查元素。

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


【解决方案1】:

请在下面尝试:

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

【讨论】:

  • 没有工作:( org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//* [text() ="לכל המבצעים"]"}
【解决方案2】:

希望这会奏效

//div[@class='home-page-actions_promotion-btn']//a

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 2015-02-04
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多