当我使用selenium webdriver java在Web应用程序中进行测试时,由于其中一个元素仍然存在,因此我尝试使用我的代码,但显示错误:
例外:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .btn-flat\ For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
但是我想选择特定的日期和时间,请用代码帮助我
java eclipse硒
driver.findElement(By.id("time")).click();
driver.findElement(By.className("dtp-btn-ok")).click();
Thread.sleep(2000);
driver.findElement(By.id("th-12")).click();
Thread.sleep(3000);
driver.findElement(By.className("btn-flat ")).click();
// driver.findElement(By.id("note")).sendKeys("Be Alert Every Time");
//driver.findElement(By.className("green")).click();
package com.s3sales.demo;
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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Leads {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("http://sssamriddhisales.com/crm");
Thread.sleep(2000);
driver.findElement(By.id("userName")).sendKeys("admin");
Thread.sleep(2000);
driver.findElement(By.id("password")).sendKeys("admin123");
Thread.sleep(2000);
driver.findElement(By.className("btn-success")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.linkText("Leads"));
Thread.sleep(1000);
Actions action = new Actions(driver);
action.moveToElement(element).moveToElement(driver.findElement(By.cssSelector("[data-id='leads']"))).click().build().perform();
Thread.sleep(2000);
driver.findElement(By.id("newLead")).click();
Thread.sleep(1000);
driver.findElement(By.id("custFirstName")).sendKeys("SUBBA");
Thread.sleep(1000);
driver.findElement(By.id("custLastName")).sendKeys("RAO");
Thread.sleep(2000);
Select service=new Select(driver.findElement(By.id("custService")));
service.selectByVisibleText("Loan Against Gold");
Thread.sleep(2000);
driver.findElement(By.id("custContactNo")).sendKeys("7799445588");
Thread.sleep(1000);
driver.findElement(By.id("custEmail")).sendKeys("mymail2subbarao@gmail.com");
Thread.sleep(1000);
driver.findElement(By.id("isCust")).click();
Thread.sleep(2000);
Select branch=new Select(driver.findElement(By.id("custBranch")));
branch.selectByVisibleText("gopal shop");
Thread.sleep(1000);
driver.findElement(By.id("addressLine1")).sendKeys("6-1-138/J , Shali Bandda");
Thread.sleep(1000);
driver.findElement(By.id("addressLine2")).sendKeys("Charminar , Hyderabad");
Thread.sleep(1000);
Select state=new Select(driver.findElement(By.id("stateName")));
state.selectByVisibleText("Telangana");
Thread.sleep(2000);
Select district=new Select(driver.findElement(By.id("districtName")));
district.selectByVisibleText("Ranga Reddy");
Thread.sleep(1000);
Select area=new Select(driver.findElement(By.id("areaName")));
area.selectByVisibleText("Uppal");
Thread.sleep(1000);
Select addType=new Select(driver.findElement(By.id("addressType")));
addType.selectByVisibleText("Home");
Thread.sleep(1000);
driver.findElement(By.id("isAddPer")).click();
Thread.sleep(1000);
Select status=new Select(driver.findElement(By.id("status")));
status.selectByVisibleText("Follow up");
Thread.sleep(1000);
//remainder-on in Leads Module
driver.findElement(By.id("time")).click();
driver.findElement(By.className("dtp-btn-ok")).click();
Thread.sleep(2000);
driver.findElement(By.id("th-12")).click();
Thread.sleep(3000);
driver.findElement(By.className("btn-flat ")).click();
// driver.findElement(By.id("note")).sendKeys("Be Alert Every Time");
//driver.findElement(By.className("green")).click();
}
}
例外:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .btn-flat\ For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
答案 0 :(得分:0)
您应该使用WebDriverWait不睡觉!
这也将帮助您解决错误...
WebDriverWait wait = new WebDriverWait(webDriver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.className("btn-flat"))).click();
编辑:
话虽这么说...这里可能只是语法错误...定位器By.className("btn-flat ")
中的className末尾有空格...在我的回答中,我已更改改为By.className("btn-flat")
...随时将其改回。
EDIT2:
在我怀疑定位器不正确之后查看了站点...
打开的iFrame不是iFrame,但其行为类似于一个...
为克服这个问题,我将定位器更改为XPath,如下所示:
(By.XPATH, "//div[@class='dtp']//*[@class='dtp-btn-ok btn btn-flat']")
class='dtp'
是iFrame,其后是元素的XPath!
希望这对您有帮助!