Send keys not working , right xpath and id (Selenium)

牧云@^-^@ 提交于 2019-12-11 15:53:57

问题


Tried sendomg keys , right relative xpath or ID used but still not working properly

Tried using absolute xpath , relative xpath also ID. Tried using selassist and chropath still not working. Could there be something preventing it?

public void LoginWebSystem() {              
    driver = new ChromeDriver();    
    driver.get("http://localhost:82");  
    WebElement email = driver.findElement(By.id("login_username"));
    email.sendKeys("superadmin");   
    System.out.println("Username Set"); 
    WebElement password = driver.findElement(By.id("login_password"));
    password.sendKeys("nelsoft121586"); 
    System.out.println("Password Set"); 
    WebElement login = driver.findElement(By.id("login_submit"));
    login.click();
    System.out.println("Login Button Clicked"); 
      String newUrl = driver.getCurrentUrl();

        if(newUrl.equalsIgnoreCase("http://localhost:82/controlpanel.php")){
            System.out.println("Login Success");
        }
        else {
            System.out.println("Login Failed");
        }   

    driver.findElement(By.partialLinkText("Product")).click();
    System.out.println("Successful in proceeding to Product Page");


    driver.findElement(By.id("createlink")).click();
    System.out.println("Successful in proceeding to Create Product by Detailed");

    driver.switchTo().alert().accept();
    System.out.println("Successful in clicking alert button");
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
}

@Test (priority=1)
public void ProductDetails() {

    WebElement product = driver.findElement(By.xpath(" //*[@id="text-product"]"));
    product.sendKeys("superadmin");

} }

Expected output should input superadmin to product textbox


回答1:


  1. You have a typo in your XPath expression:

    WebElement product = driver.findElement(By.xpath(" //*[@id="text-product"]"));
                                                      ^ remove this space break
    
  2. It's better to use By.Id locator strategy where possible as this is the fastest and the most robust way of identifying elements in DOM

  3. Consider using Explicit Wait to ensure that the element is present and can be interacted with as it might be the case the element becomes available after document.readyState becomes complete. Check out How to use Selenium to test web applications using AJAX technology article for more detailed explanation.

    new WebDriverWait(driver, 10)
            .until(ExpectedConditions.elementToBeClickable(By.id("text-product")))
            .click();
    
  4. Make sure that your selector matches an <input> because if the id belongs to other element type like <div> it doesn't make a lot of sense to send keys there.



来源:https://stackoverflow.com/questions/57323693/send-keys-not-working-right-xpath-and-id-selenium

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!