51Testing软件测试论坛

标题: 关于java+selenium启动报错问题,求大神指导 [打印本页]

作者: rongxiah    时间: 2016-4-13 20:36
标题: 关于java+selenium启动报错问题,求大神指导
package com.selenium.base;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.*;

public class fist {
  
   private static String url="http://www.baidu.com/";
  
   public static void main(String args[]){
     
           System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
           WebDriver driver = new ChromeDriver();   
    driver.get(url);
    String targeturl=driver.getCurrentUrl();
    System.out.println(targeturl);
    //String pagesource = driver.getPageSource();
    //System.out.println(pagesource);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
   
    WebElement element = driver.findElement(By.xpath(".//*[@id='su']"));   

eclipse运行后报错,可是xpath的路径根本就不是我程序中配置的,是从哪里读取的?
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=C:\Users\ADMINI~1\AppData\Local\Temp\scoped_dir13376_117}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=46.0.2490.86, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 80e82616e498115d7ef9262953093a7c
*** Element info: {Using=xpath, value=//*[@id='index-page']/footer/div[3]/ul/li[2]/a/span}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
        at com.selenium.base.fist.main(fist.java:25)

作者: tmmjmi    时间: 2016-4-14 14:08
driver.findElement(By.xpath(".//*[@id='su']")) 改成
driver.findElement(By.xpath("//*[@id='su']"));   
作者: 清晨一缕阳光    时间: 2016-4-15 08:53
可以使用ID去定位啊,不一定所有的元素都要使用xpath去定位,如果有ID,name等属性的时候可以考虑直接用这些定位。




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2