【问题标题】:Selenium - Java - Sendkeys not working in textfield. No errorSelenium - Java - Sendkeys 在文本字段中不起作用。没有错误
【发布时间】:2021-01-08 16:17:15
【问题描述】:

我在 Eclipse 环境中使用 Selenium 使用 Java 来自动化网站上的测试用例。这是为了个人学习。 以下是html代码。

我在 Maven 项目中使用页面对象模型。当我尝试在文本字段上使用 sendkeys 方法时,它不起作用。没有错误。测试用例成功通过。但是没有在文本字段中输入文本。我尝试在输入文本之前单击文本字段。它确实单击,但没有键入任何内容。可能是什么问题呢。硒版本 - 3.141。 Eclipse 版本 - 4.7。以下是我的页面文件中的代码

package seleniumeasy.qa.Page;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import seleniumeasy.qa.Base.Base;

public class tblDataSearchPage extends Base{

    @FindBy(xpath="//div[@class='panel-body']/input")
    WebElement txtSearch;
    
    public tblDataSearchPage()
    {
        PageFactory.initElements(driver, this);
    }
    
    public void searchElement()
    {
        //txtSearch.click();
        txtSearch.clear();
        txtSearch.sendKeys("smith");
        txtSearch.sendKeys(Keys.RETURN);
                
    }
}

以下是我的测试课

package seleniumeasy.test.Tests;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import seleniumeasy.qa.Base.Base;
import seleniumeasy.qa.Page.tblDataSearchPage;
import seleniumeasy.qa.Page.tblPaginationPage;

public class tblDataSearchTest extends Base
{

    SoftAssert sAssert;
    tblPaginationPage tObj;
    tblDataSearchPage obj;
    
    public tblDataSearchTest()
    {
        Init();
        sAssert = new SoftAssert();
        tObj=new tblPaginationPage();
        obj = tObj.clickTableDataSearchMenu();      
    }
    @Test
    public void verifySearchElement()
    {
        obj.searchElement();
    }
    /*@Test
    public void verifySearchElementBasedOnUsername()
    {
        boolean bTestPass = obj.searchFilterName();
        
        
    }*/
}

【问题讨论】:

  • 请添加你的测试类
  • 添加了测试类
  • 进一步调查,这似乎只是 chrome 驱动程序的问题。在 Firefox 中它工作正常。
  • 现在我下载了最新的chromedriver(87.0.4280.88)运行程序还是一样的问题。

标签: html selenium sendkeys pageobjects


【解决方案1】:
txtSearch.click()
WebElement currentElement = driver.switchTo().activeElement();
currentElement.sendKeys("something")

你可以试试这个

【讨论】:

  • @Jay 你也可以通过点击勾号来接受答案
猜你喜欢
  • 2021-11-21
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多