gpt4 book ai didi

java - 如何在 selenium Web 驱动程序中提取或选择剩余元素上的特定日期和时间

转载 作者:太空宇宙 更新时间:2023-11-04 09:54:48 25 4
gpt4 key购买 nike

当我使用 selenium webdriver java 在我的 Web 应用程序中进行测试时,其中一个元素是剩余的,我尝试使用我的代码,但它显示错误:

异常(exception):

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 Selenium

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();

包 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):

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

最佳答案

您应该使用WebDriverWait不 sleep !

这也将帮助您解决错误...

WebDriverWait wait = new WebDriverWait(webDriver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.className("btn-flat"))).click();

编辑:

话虽如此...这可能只是一个语法错误...您的定位器 By.className("btn-flat ") 在 className 末尾有一个空格...在我的回答中,我已将其更改为 By.className("btn-flat")...请随意将其改回来。

编辑2:

查看该网站后,我怀疑定位器不正确......

打开的 iFrame 不是 iFrame,但它的行为就像一个...

为了克服这个问题,我已将定位器更改为 XPath,如下所示:

(By.XPATH, "//div[@class='dtp']//*[@class='dtp-btn-ok btn btn-flat']")

class='dtp' 是 iFrame,后面的内容是元素的 XPath!

希望这对您有帮助!

关于java - 如何在 selenium Web 驱动程序中提取或选择剩余元素上的特定日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54290255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com