How to locate and type something in the textbox

后端 未结 4 2019
旧巷少年郎
旧巷少年郎 2020-12-12 06:03
public class testFluent {   

WebDriver driver;   
    @Before
        public void setUp(){        
    driver = new FirefoxDriver();
    driver.manage().window().ma         


        
相关标签:
4条回答
  • 2020-12-12 06:41

    For wait you can use something like this

        private boolean wAit(String match)
        {
        try
            {
            (new WebDriverWait(driver, 30))
                .until(ExpectedConditions.presenceOfElementLocated (By.xpath(match)));
            return true;
            }
        catch (NoSuchElementException e) {
            return false;
        }
        }
    

    You can create the above method and use it where ever you need to wait for an element. for example

    if want a write something in the textbox and want to wait for the text box to load

    wAit(" xpath of the textbox here")
    driver.findelements... sendkeys()..
    

    If you want you can change the locator type and increase/decrease the time limit also

    0 讨论(0)
  • 2020-12-12 06:49

    I hope this helps u..

    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.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;  
    
    
    public class yahoo {  
    
         FirefoxDriver Driver=null;
         WebDriverWait wait = null;
    
    
    
         @BeforeTest  
         public void start(){  
          Driver = new FirefoxDriver();  
         }  
    
        @Test 
         public void Test() throws InterruptedException{   
          wait=new WebDriverWait(Driver,90); 
          System.out.println("Loading yahoo search page");  
          Driver.get("http://www.yahoo.com");  
          System.out.println("Yahoo search page loaded fine");  
          WebElement text=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='p_13838465-p']")));
          text.sendKeys("selenium");
          Thread.sleep(5000);
    
         }  
    
         @AfterTest  
         public void close(){  
         Driver.quit();   
         }  
        }  
    
    0 讨论(0)
  • 2020-12-12 06:56

    I encountered with same issue and couldn't find any solution for it.Then i tried WebDriverWait instead of FluentWait and it worked for me

    WebDriverWait wait = new WebDriverWait(Driver, 60);
    wait.withTimeout(60, TimeUnit.SECONDS);
    wait.pollingEvery(5, TimeUnit.SECONDS);
    wait.ignoring(NoSuchElementException.class);
    wait.until(new ExpectedCondition<Boolean>(){
          @Override
          public Boolean apply(WebDriver driver) {
              WebElement ele=driver.findElement(locator);
              if(ele==null)
                  return false;
              else
              {
                  System.out.println("WebElement found");
                  return true;
              }
    
          }
        });
    
    0 讨论(0)
  • 2020-12-12 07:05

    Regarding your By.id statement, it looks like you have passed an XPath instead of just the id. So this:

    element = myDynamicElement(By.id("//*[@id='p_13838465-p']")); //this locator is an XPath
    

    should become this:

    element = myDynamicElement(By.id('p_13838465-p')); //this is just the ID
    

    However, if this ID is dynamically generated, then it won't work reliably, and you may need to considering finding it by a different locator.

    Then, once you've identified the element, to type in it, you use .sendKeys("your text here"), like this:

    element.sendKeys("your text here");
    

    or you could combine it into one line by skipping the element = part and just say:

    myDynamicElement(By.id('p_13838465-p')).sendKeys("your text here");
    
    0 讨论(0)
提交回复
热议问题