无法将密钥发送到selenium中的用户名框

时间:2017-10-10 20:08:01

标签: selenium selenium-webdriver webdriver

我已尝试过多次使用不同的xpath并在不同的论坛中询问但是没有得到解决方案。等待你的帮助。

package chromedriver;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Espncom {

   public static void main(String[] args) {
      // TODO Auto-generated method stub

      System.setProperty("webdriver.gecko.driver","C:\\Selenium softwares\\geckodriver-v0.19.0-win64\\geckodriver.exe");

      WebDriver driver= new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("http://www.espn.com/");   
      driver.findElement(By.linkText("Log In")).click();

      //driver.findElement(By.xpath("//input[@placeholder='Username or Email Address']")).sendKeys("cricket");
      driver.findElement(By.xpath(".//div/div/section/section/form/section/div[1]/
      div/label/span[2]/input")).sendKeys("cricket");

   }
}

用户名或电子邮件地址的HTML代码:

<input placeholder="Username or Email Address" autocapitalize="none" autocomplete="on" autocorrect="off" spellcheck="false" ng-model="vm.username" ng-pattern="/^[^<&quot;>]*$/" ng-required="true" did-disable-validate="" ng-focus="vm.resetUsername()" class="ng-pristine ng-invalid ng-invalid-required ng-valid-pattern ng-touched" tabindex="0" required="required" aria-required="true" aria-invalid="true" type="email">

1 个答案:

答案 0 :(得分:1)

在Espn网站中,当您单击登录时,它会打开一个框架。你需要切换到框架,然后你需要尝试找到电子邮件或名称文本框。 尝试使用以下代码并更新回来

  driver.switchTo().frame("disneyid-iframe");
  driver.findElement(By.xpath("//input[@placeholder='Username or Email address']")).sendKeys("cricket");