package MonitorTest; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; 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.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; public class MonitorTest_improve { public static void main(String[] args) throws InterruptedException, IOException{ //存储监控 名称的文件地址 String fileUrl="D:\\bukejian\\tmp.txt"; //访问地址 String url=; ArrayList<String> list = new ArrayList<String>(); String encoding="GBK"; File file = new File(fileUrl); if(file.isFile() && file.exists()){ InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ list.add(lineTxt); } read.close(); }else{ System.out.println("No exist"); } System.setProperty("webdriver.firefox.bin", "D:/bukejian/Mozilla Firefox/firefox.exe"); WebDriver dr = new FirefoxDriver(); dr.get(url); WebElement submit=(new WebDriverWait(dr,10)).until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver d){ return d.findElement(By.cssSelector("input[type=\"submit\"]")); } }); dr.findElement(By.name("login_username")).clear(); dr.findElement(By.name("login_username")).sendKeys("bukejian"); dr.findElement(By.name("login_password")).clear(); dr.findElement(By.name("login_password")).sendKeys("bukejian"); submit.click(); int j=0; for(String a :list){ (new WebDriverWait(dr,10)).until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver d){ return d.findElement(By.linkText("Add")); } }).click(); WebElement select=(new WebDriverWait(dr,10)).until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver d){ return d.findElement(By.name("type_select")); } }); new Select(select).selectByVisibleText("Graph"); WebElement graph_id=(new WebDriverWait(dr,10)).until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver d){ return d.findElement(By.id("local_graph_id")); } }); new Select(graph_id).selectByVisibleText(a); (new WebDriverWait(dr,10)).until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver d){ return d.findElement(By.cssSelector("input[type=\"button\"]")); } }).click(); //dr.findElement(By.cssSelector("input[type=\"submit\"]")).click();//提交 System.out.println(j+"="+a); j++; } dr.close(); } }
全加上了 等待10s,用取消操作验证了下倒是不会超时报错了
Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: {“method”:”bukejian”,”selector”:”bukejian”}
之前会报这种错误,应该是没找到元素超时了,这次所有元素都最多等待10s应该不会报错了吧
转载于:https://my.oschina.net/u/571166/blog/273048
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109810.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...