我正在编写一个selenium代码来执行以下操作。
I'm writing a selenium code to do the below thing.
当我这样做时,我会得到一个结果列表,我想得到第一个结果块的标题。
When I do this, I will get a list of results and I want to get the heading of the first result block.
以下是我的代码。
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("www2.chubb/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,我可以在我的控制台中获得标题名称。
In my code, I'm able to get step1, 2, 3 done and I able to get the title name in my console.
在尝试获取标题文本时,它给了我以下异常。
It is giving me the below exception when trying to get the heading text.
JavaScript错误: www2.chubb/us-en/find-agent-page.aspx ,第2行:SyntaxError:expected expression,got' <'
JavaScript error: www2.chubb/us-en/find-agent-page.aspx, line 2: SyntaxError: expected expression, got '<'
JavaScript警告: www2.chubb/_Global-Assets/js/jquery-webdriver.js ,第1行:使用// @表示sourceMappingURL编译指示已弃用。使用//#代替[[FirefoxDriver:firefox on XP (320d5e47-8575-4566-9622-d8275cf72ded)] - > xpath: .//* [@ id ='chubbAgentData '] / li / h2]
JavaScript warning: www2.chubb/_Global-Assets/js/jquery-webdriver.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead [[FirefoxDriver: firefox on XP (320d5e47-8575-4566-9622-d8275cf72ded)] -> xpath: .//*[@id='chubbAgentData']/li/h2]
请告诉我哪里出错了,我该如何解决这个问题。
Please let me know where am I going wrong and how can I fix this.
推荐答案你不应该使用 toString()方法 - 使用 getText():
You should not be using toString() method - use getText():
driver.findElement(By.xpath(".//*[@id='chubbAgentData']/li/h2")).getText();更多推荐
获得价值无法理解
发布评论