动作类 - 文本在浏览器的地址栏输入,而不是谷歌搜索框

时间:2015-10-17 19:33:46

标签: selenium selenium-webdriver

我正在尝试在Google搜索框中输入文字,但文本会在浏览器的地址栏中输入。我在代码中使用的xpath是google主页上的搜索框。以下是我执行的代码:

package p1;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;

public class ClickLink 
{
    static WebDriver driver;
    public static void main(String[] args)
    {
        try
        {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.co.in/");
        WebElement searchbox = driver.findElement(By.xpath(".//*[@id='sb_ifc0']//input"));
        Actions ob = new Actions(driver);
        ob.moveToElement(searchbox).doubleClick().sendKeys("Akash").build().perform();
        System.out.println("Text entered in Google search box !!");
        Thread.sleep(5000);
        driver.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred : "+e);
            driver.close();
        }
    }
}

以下是显示正在执行的上述代码的视频:[link]

请让我知道代码无法按预期运行的原因。另请注意,我想使用'Actions'类来实现此目的。

2 个答案:

答案 0 :(得分:1)

以下代码在我的结尾处理正常

package stackOverflow;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Ans {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        driver.manage().window().maximize();
        driver.get("https://www.google.co.in/");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("Akash");
        System.out.println("Text entered in Google search box !!");
        Thread.sleep(5000);
        driver.close();

    }

}

您可以使用webdriver元素定位器(Firefox插件)来了解任何Web元素xpath

答案 1 :(得分:0)

我处理了你的问题(虽然我是Selenium的初学者)。我只是试图创建一个解决方法。请确定以下代码是否适合您。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class actionclassgooglestackoverflowquestion {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        //this code is used to fix the below issue
        //http://stackoverflow.com/questions/33190740/actions-class-text-gets-entered-at-the-address-bar-of-the-browser-instead-of-go

        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.google.co.uk/");
        driver.manage().window().maximize();
        WebElement element=driver.findElement(By.xpath("//input[@id='lst-ib']"));
        element.click();
        Actions act1=new Actions(driver);
        act1.moveToElement(element).sendKeys("Overflow").build().perform();
        driver.close();



    }

}