Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

html - Selenium - Java - Sendkeys not working in textfield. No error

I am using Selenium using Java in Eclipse environment to automate testcases on website. This is for personal learning. Following is the html code.

enter image description here

I am using Page Object Model in Maven project. When I try to use sendkeys method on the textfield it does not work. There is no error. The testcase passes successfully. But text is not typed in the textfield. I tried clicking on the textfield before entering text. It does click but it does not type anything. What could be the problem. Selenium Version - 3.141. Eclipse version - 4.7. Following is the code in my Page file

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);
                
    }
}

Following is my test class

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();
        
        
    }*/
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
txtSearch.click()
WebElement currentElement = driver.switchTo().activeElement();
currentElement.sendKeys("something")

Could you try this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...