org.openqa.selenium.InvalidElementStateException:用于谷歌搜索

时间:2013-11-27 03:45:58

标签: java xpath selenium-webdriver

我正在尝试在Google搜索中输入手机,但在搜索时发送手机时,以下代码失败了。我用了下面的xpath。请在邮件结尾处查看错误。

我甚至尝试过如下的其他xpath:

html/body/div[1]/div[3]/div[1]/div[1]/div[3]/div/div/div/form/fieldset[2]/div/div/div/table/tbody/tr/td[2]/div/input[3]

//div[@id='gs_lc0']/input[@id='gs_htif0']


public class exercise1 {

    @Test
    public void googleExercise() throws InterruptedException    {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://google.com");
        Thread.sleep(10000);
        driver.findElement(By.xpath("//*[@id='gs_tti0']")).sendKeys("Mobile Phones");
        driver.findElement(By.xpath("//*[@id='gbqfba']']")).click();
        List<WebElement> alllinks = driver.findElements(By.tagName("a"));
        System.out.println(alllinks.size());
        for (int i=0;i<alllinks.size();i++) {
            System.out.println("Link " + i + " = " + alllinks.get(i).getText() + " = " + alllinks.get(i).isDisplayed());
        }
    }
}

我收到以下错误:

org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions
Command duration or timeout: 46 milliseconds
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: 'ACHAYAPA-LAP', ip: '10.177.147.32', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Session ID: 37b84f2c-0735-4a6b-93e0-329bd2760c39
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=17.0.11}]
    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:193)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:89)
    at exercise1.googleExercise(exercise1.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is disabled and so may not be used for actions
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: 'ACHAYAPA-LAP', ip: '10.177.147.32', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: driver.version: unknown
    at <anonymous class>.fxdriver.preconditions.enabled(file:///C:/Users/achayapa/AppData/Local/Temp/anonymous4609315224719991191webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:8216)
    at <anonymous class>.DelayedCommand.checkPreconditions_(file:///C:/Users/achayapa/AppData/Local/Temp/anonymous4609315224719991191webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10827)
    at <anonymous class>.DelayedCommand.executeInternal_/h(file:///C:/Users/achayapa/AppData/Local/Temp/anonymous4609315224719991191webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10844)
    at <anonymous class>.DelayedCommand.executeInternal_(file:///C:/Users/achayapa/AppData/Local/Temp/anonymous4609315224719991191webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10849)
    at <anonymous class>.DelayedCommand.execute/<(file:///C:/Users/achayapa/AppData/Local/Temp/anonymous4609315224719991191webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10791)

3 个答案:

答案 0 :(得分:0)

请参阅,如果这可能有帮助,

@Test
public void googleExercise() throws InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://google.com");
Thread.sleep(10000);
driver.findElement(By.name("q")).sendKeys("Mobile Phones");
Thread.sleep(5000);
WebElement findElementsClass = firefoxDriver.findElement(By.className("gssb_m"));
List<WebElement> alllinks = findElementsClass.findElements(By.tagName("span"));
System.out.println(alllinks.size());
for (int i=0;i<alllinks.size();i++) {
    System.out.println("Link " + i + " = " + alllinks.get(i).getText() + " = " + alllinks.get(i).isDisplayed());
}
}

答案 1 :(得分:0)

使用Google进行测试非常糟糕,我尝试了使用其id,xpath等将一些搜索文本发送到谷歌搜索文本框的不同方法。 每次禁用元素和测试失败。 最后改变了查找机制 driver.findElement(By.name(&#34; q&#34;))。sendKeys(&#34; some text&#34;); 这很有效,谢谢。

答案 2 :(得分:0)

enter code heredriver.get("http://google.com");
    Thread.sleep(4000);
    driver.findElement(By.name("q")).sendKeys("selenium");
    driver.findElement(By.name("btnG")).click();

    Thread.sleep(4000);

    String p1 = "//*[@id='rso']/div[2]/li[" ;
    String p2 = "]/div/h3/a";

    int i = 1;
    while(checkElementPresent(p1+i+p2))
    {
        String x = driver.findElement(By.xpath(p1+i+p2)).getText();
        System.out.println(x);

        i++;
    }

}

public static boolean checkElementPresent(String path)
{
    int count = driver.findElements(By.xpath(path)).size();

    if(count == 0)
    {
        return false;

    }

    else
    {
        return true;
    }

}