我不得不重新测试xpath,以前工作正常,但现在却出现了错误。
xpath
我也尝试过使用其他定位器id,例如name。但仍然出现相同的错误。
id
name
package staging; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class login { public static void main (String[]args){ System.setProperty("webdriver.gecko.driver","C:\\Program Files\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //opening the browser driver.get("https://staging.keela.co/login"); //logging driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela"); driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click(); } }
当您访问url时,https://staging.keela.co/login有一个阻止UI的Ajax加载器,因此我们必须等待Ajax加载器完成所有WebElement的加载,并且emailand password字段变为可见。为了实现这一目标,我们将推出ExplicitWait即WebDriverWait与ExpectedConditions设置为elementToBeClickable对emailfield.Here是工作代码块:
https://staging.keela.co/login
email
password
ExplicitWait
WebDriverWait
ExpectedConditions
elementToBeClickable
System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://staging.keela.co/login"); WebDriverWait wait = new WebDriverWait (driver, 15); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='login-email']"))); element.sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela"); driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click();