【问题标题】:Selenium clicks on Browser's url rather than search box of websiteSelenium 点击浏览器的 url 而不是网站的搜索框
【发布时间】:2015-07-09 19:08:06
【问题描述】:

我正在尝试自动化 Amazon.in 的搜索框,当我尝试在那里输入一些字符串时,它会指向浏览器的地址栏。我的代码也是一样的。

注意-我已经尝试过使用 firebug 和标签遍历的不同 xpath。

另外请告诉我为什么我们必须使用 always build and perform 方法和操作?

public static void main(String args[])
{
    WebDriver driver= new FirefoxDriver();
    driver.get("http://amazon.in");
    Actions action=new Actions(driver);
    WebElement element= driver.findElement(By.xpath(".//*[@id='nav-link-yourAccount']/span[2]"));
    action.moveToElement(element).build().perform();
    WebElement search= driver.findElement(By.xpath(".//*[@id='twotabsearchtextbox']"));
    action.keyDown(Keys.SHIFT).moveToElement(search).sendKeys("teststring").build().perform();
    action.contextClick(search).build().perform();
}

【问题讨论】:

  • 有谁可以解决这个问题?

标签: java selenium xpath


【解决方案1】:
    public static void main(String args[]) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://amazon.in");
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        Actions action = new Actions(driver);
        WebElement search = driver.findElement(By.xpath(".//*[@id='twotabsearchtextbox']"));

        //Search using actions by combining entering search string and then hit enter
        action.click(search).sendKeys("Test").sendKeys(Keys.RETURN).build().perform();

        // This also works where it does the same without actions class
        search.sendKeys("test");
        search.sendKeys(Keys.RETURN);
    }

在您的代码中:

下一行在浏览器搜索而不是亚马逊搜索栏中输入 teststring,因为您只是移动到该元素而不是单击它。 action.keyDown(Keys.SHIFT).moveToElement(search).sendKeys("teststring").build().perform();

这就像右键单击/上下文单击搜索栏 action.contextClick(search).build().perform();


来自 API 文档:

build() 生成一个包含到目前为止所有动作的复合动作, 准备好执行(并重置内部构建器状态,所以 随后对 build() 的调用将包含新的序列)。

perform() 执行操作的便捷方法,无需 首先调用 build()

请阅读以下链接以获得清晰的图片:
LinkOne
LinkTwo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2013-05-20
    • 2015-04-13
    • 1970-01-01
    • 2017-02-26
    • 2020-05-03
    相关资源
    最近更新 更多