gpt4 book ai didi

java - 使用 webdriver/java 从 txt 文件读取字符串并将其与下拉列表中的项目进行比较

转载 作者:行者123 更新时间:2023-12-01 10:40:47 24 4
gpt4 key购买 nike

这是场景:

1) 创建一个输入字符串=Sep 2015 的文件2)将下拉列表收集到数组中3) 如果数组等于字符串则退出循环,否则下载新的月份报告并用新的月份名称覆盖 txt 文件。

我尝试了下面的代码,但无法实现txt比较部分和txt覆盖部分,请帮忙。

driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
//maximizing the window
driver.manage().window().maximize();

List<WebElement> options;
int i = 0;
do
{
options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
if(options.get(i).getText().equals("Sep 2015 (Unconventional wells)"))
{

System.out.println("old month");
break;
}
else
{ if (options.get(i).getText().equalsIgnoreCase("All" )){

System.out.println("Download new month");

WebElement identifier = driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
Select select1 = new Select(identifier);

//select1.selectByVisibleText("Oct");

select1.selectByVisibleText("Oct 2015 (Unconventional wells)");


Wait(20000);
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
Wait(70000);
//Click on File save button
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
//wait time to load the options
Wait(20000);
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
//fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
//fprofile.setPreference( "pdfjs.disabled", true );
Wait(10000);
String str=options.get(2).getText();
System.out.println("str: " + str);

// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Oct month data downloaded in csv format");
//System.out.println("New month");
}
} } while (i++ < options.size());
}

最佳答案

一旦尝试这样:

//全局变量:

private WebDriver driver;
private String fileName = "/home/saritha/Desktop/MySeleniumFile.txt";
private File file;

在测试方法中:

@Test
public void oilGasTestng() throws InterruptedException {
driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
WebElement mSelectElement = driver
.findElement(By
.xpath("//select[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
List<WebElement> optionsList = mSelectElement.findElements(By
.tagName("option"));

for (int i = 2; i < optionsList.size(); i++) {
WebElement element = optionsList.get(i);
String newMonth = element.getText();
/*
* First we have read the data from file, if the file is empty then
* download the file and save the downloaded month(which is old
* month when v done with the downloading).
*/
String oldMonth = "";
if (i > 2) {
oldMonth = getTheOldMonthFromFile();
}
System.out.println("Old Month= " + oldMonth + " NewMonth= "
+ newMonth);
if (newMonth.equals(oldMonth)) {
// IF the string are same, nthng we need to do
} else if (!newMonth.equals(oldMonth)) {
/*
* If the string are not same,then i.e., considered as new
* Month, download the new month details
*/
element.click();
driver.findElement(
By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']"))
.click();
System.out.println(newMonth
+ " month data downloaded in csv format");
saveIntoAFile(newMonth);
/*
* You can which is oldMonth which is new month, by unCommenting
* below condition
*/
// if (i == 4)
break;
}
}
}


//Save data into a file
private void saveIntoAFile(String oldMonth) {
BufferedWriter bw = null;
if (oldMonth != null) {
file = new File(fileName);
try {
if (!file.exists()) {
file.createNewFile();
}
Writer writer = new FileWriter(file);
bw = new BufferedWriter(writer);
bw.write(oldMonth);
} catch (IOException e) {
e.printStackTrace();
} finally {

try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

//Get the oldMonth string from the file
private String getTheOldMonthFromFile() {
if (file == null && !file.exists()) {
return null;
}
String oldMonth = "";
StringBuffer strBuffer = new StringBuffer();
BufferedReader br = null;
java.io.FileReader reader = null;
try {
reader = new java.io.FileReader(file);
br = new BufferedReader(reader);
while ((oldMonth = br.readLine()) != null) {
strBuffer.append(oldMonth);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return strBuffer.toString();
}

关于java - 使用 webdriver/java 从 txt 文件读取字符串并将其与下拉列表中的项目进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414292/

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