获得价值无法理解

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

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

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

以下是我的代码。

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); } } 

在我的代码中,我能够完成step1,2,3,并且我能够在控制台中获取标题名称。

在尝试获取标题文本时,它给出了以下exception。

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

JavaScript警告: https ://www2.chubb.com/_Global-Assets/js/jquery-webdriver.js,第1行:不建议使用// @来表示sourceMappingURL pragma。 使用//#而不是[[FirefoxDriver:firefox on XP(320d5e47-8575-4566-9622-d8275cf72ded)] – > xpath:.// * [/ id =’chubbAgentData’] / li / h2]

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

你不应该使用toString()方法 – 使用getText()

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