【问题标题】:Unable to understand on getting the value无法理解获得价值
【发布时间】:2017-05-06 12:07:56
【问题描述】:

我正在编写一个 selenium 代码来执行以下操作。

  1. 在文本框中输入值。
  2. 选择下拉值。
  3. 选择一个单选按钮。
  4. 点击开始按钮。

当我这样做时,我会得到一个结果列表,我想得到第一个结果块的标题。

下面是我的代码。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\home\\Downloads\\geckodriver.exe");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        driver = new FirefoxDriver(capabilities);
        driver.get("https://www2.chubb.com/us-en/find-agent-page.aspx");

        driver.findElement(By.xpath(".//*[@id='tbAddress']")).sendKeys("60089");
        driver.findElement(By.xpath(".//*[@id='cphHeroContent_drpDistanceMiles']")).sendKeys("2");
        driver.findElement(By.xpath(".//*[@id='cphHeroContent_rdType_0']")).click();
        driver.findElement(By.xpath(".//*[@id='cphHeroContent_btnSearch']")).click();

        String title = driver.getTitle().toString();
        System.out.println(title);

        Thread.sleep(10000L);

        String getHeadingTitle = driver.findElement(By.xpath(".//*[@id='chubbAgentData']/li/h2")).toString();
        System.out.println(getHeadingTitle);

    }
}

在我的代码中,我可以完成第 1、2、3 步,并且可以在控制台中获取标题名称。

尝试获取标题文本时出现以下异常。

JavaScript 错误:https://www2.chubb.com/us-en/find-agent-page.aspx, 第 2 行:语法错误:预期的表达式,得到 '

JavaScript 警告: https://www2.chubb.com/_Global-Assets/js/jquery-webdriver.js,第 1 行: 不推荐使用 //@ 来指示 sourceMappingURL 杂注。采用 //# 而是 [[FirefoxDriver:XP 上的 firefox (320d5e47-8575-4566-9622-d8275cf72ded)] -> xpath: .//*[@id='chubbAgentData']/li/h2]

请让我知道我哪里出错了,我该如何解决这个问题。

【问题讨论】:

    标签: java selenium xpath automated-tests


    【解决方案1】:

    您不应该使用toString() 方法 - 使用getText()

    driver.findElement(By.xpath(".//*[@id='chubbAgentData']/li/h2")).getText();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      相关资源
      最近更新 更多