Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何不用java硬编码程序?已使用的属性文件。但无法将其用于多个XPath?_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

如何不用java硬编码程序?已使用的属性文件。但无法将其用于多个XPath?

如何不用java硬编码程序?已使用的属性文件。但无法将其用于多个XPath?,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,场景:从站点下载新的月报 月:2015年10月非常规井 结构如下: config.properties: #browser browser=http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report #drop-down box list_box= .//*[@id='ReportVie

场景:从站点下载新的月报

月:2015年10月非常规井

结构如下:

config.properties:

#browser
browser=http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report
#drop-down box
list_box= .//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']
#view report button
view_report = .//*[@id='ReportViewerControl_ctl04_ctl00']
#save button
save_button = .//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']
#csv button
csv_button = .//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a
#read path
read_path = E:\\Ashik\\wkspSelenium\\PropertyPractice\\src\\package1\\Month.txt
#write path
write_path = E:\\Ashik\\wkspSelenium\\PropertyPractice\\src\\package1\\Month.txt
Implementation.java:

package package1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

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.firefox.FirefoxProfile;

import package1.Fileaccessing;

public class Implementaion {

    public static  WebDriver driver;
    public  FileInputStream fis;
    public static String propertyfilepath="config.properties";
    public String browserName;

    //To get a key value from property file
    public String getProperty(String key) throws IOException, FileNotFoundException{
        fis=new FileInputStream(propertyfilepath);
        Properties prop=new Properties();
        prop.load(fis);
        return prop.getProperty(key);

    }
    public  static void initiate_webdriver() throws IOException
    {   
         // Changing default file downloading location  path using the FirefoxProfile.setpreference method.
          FirefoxProfile fprofile = new FirefoxProfile();   
          fprofile.setPreference("browser.download.folderList",2);
          fprofile.setPreference("browser.download.manager.showWhenStarting",false);
          //file download path
          fprofile.setPreference("browser.download.dir", "E:\\"); //need to ask muthu
          //CSV format to download the data
          fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
          //Automatically downloads the file without manual selection
          fprofile.setPreference("browser.helperApps.alwaysAsk.force",false);
          fprofile.setPreference("browser.download.manager.showAlertonComplete",false);
          fprofile.setPreference("browser.download.manager.closeWhenDone",false);
          //Assigning the created profile to Firefox driver

          WebDriver driver=new FirefoxDriver(fprofile);
    }

    //To get a url in browser
        public  static void get_url(String link) throws InterruptedException
        {


            driver.get(link);
            driver.manage().window().maximize();
        }

        //To select a value by visible text in a drop down


         public static void downloads_new_month(String xpath, String value)  throws InterruptedException
         {
              WebElement mSelectElement = driver.findElement(By.xpath(xpath));
              List<WebElement> optionsList = mSelectElement.findElements(By.tagName("option"));
              Fileaccessing fileAccessObject = new Fileaccessing();
              //Reading txt from the Month txt file
              String oldMonth = fileAccessObject.getTheOldMonthFromFile(propertyfilepath);
              System.out.println("The old month is: " + oldMonth);
              String newMonth =""; 

              for (int i = 2; i < optionsList.size(); i++) {

              WebElement element = optionsList.get(i);
              newMonth = element.getText();

              //Message that prints the new month
              System.out.println("The new month is:"+newMonth);


              /*Condition to check if the New month is equal to Old month, if it is not equal then proceeds
               * to download that particular month data or else breaks the loop
               */

              if (oldMonth.equals(newMonth)) {
              System.out.println("No new months are available to download");
              driver.close();
              break;
              }else if (i==2 & !(oldMonth.equals(newMonth))) {
              //else if (!(oldMonth.equals(newMonth))) {    
              element.click();
                //Click on View Report button
              driver.findElement(By.xpath(xpath)).click();
                //Wait time to load the selected month data
              Wait(20000);} 
              public static   saveButton(String xpath2 , String value2)   throws InterruptedException{
                //Click on File save button
                driver.findElement(By.xpath(xpath2)).click();
                //wait time to load the File format options
              Wait(20000); } 
         public static void csvButton(String xpath3 , String value3) throws InterruptedException{
                //Click on csv format button
              driver.findElement(By.xpath(xpath3)).click();
              Wait(10000);
                //Success message to indicate that the data is downloaded successfully in csv format
              System.out.println("New Month data downloaded in csv format:" + newMonth);
         }
              //Saves the new month txt to the file
         public static void save_new_month() {
              fileAccessObject.saveIntoAFile(newMonth, propertyfilepath);
              //closes the web page once the file is downloaded
              driver.close();
              break;
         }
        } }



         public static void Wait(int time){

            try {
                Thread.sleep(time);
                } catch (Exception e) {
        // TODO: handle exception
        } } 
}
package1;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.List;
导入java.util.Properties;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.firefox.FirefoxProfile;
导入包1.文件访问;
公共类实现{
公共静态WebDriver;
公共文件输入流fis;
公共静态字符串propertyfilepath=“config.properties”;
公共字符串浏览器名;
//从属性文件获取键值的步骤
公共字符串getProperty(字符串键)引发IOException、FileNotFoundException{
fis=新文件输入流(propertyfilepath);
Properties prop=新属性();
道具荷载(fis);
返回prop.getProperty(键);
}
public static void initiate_webdriver()引发IOException
{   
//使用FirefoxProfile.setpreference方法更改默认文件下载位置路径。
FirefoxProfile fprofile=新的FirefoxProfile();
fprofile.setPreference(“browser.download.folderList”,2);
fprofile.setPreference(“browser.download.manager.showWhenStarting”,false);
//文件下载路径
fprofile.setPreference(“browser.download.dir”,“E:\\”;//需要询问muthu
//CSV格式下载数据
fprofile.setPreference(“browser.helperApps.neverAsk.saveToDisk”、“text/csv”);
//自动下载文件,无需手动选择
fprofile.setPreference(“browser.helperApps.alwaysAsk.force”,false);
fprofile.setPreference(“browser.download.manager.showartoncomplete”,false);
fprofile.setPreference(“browser.download.manager.closeWhenDone”,false);
//将创建的配置文件分配给Firefox驱动程序
WebDriver=新的FirefoxDriver(fprofile);
}
//在浏览器中获取url的步骤
公共静态void get_url(字符串链接)引发InterruptedException
{
获取(链接);
driver.manage().window().maximize();
}
//通过下拉列表中的可见文本选择值的步骤
公共静态void下载\u new\u month(字符串xpath,字符串值)引发InterruptedException
{
WebElement mSelectElement=driver.findelelement(By.xpath(xpath));
列表选项列表=mSelectElement.findElements(按.tagName(“选项”));
FileAccess fileAccessObject=新建FileAccess();
//从月份txt文件中读取txt
字符串oldMonth=fileAccessObject.getTheOldMonthFromFile(propertyfilepath);
System.out.println(“旧月份为:“+oldMonth”);
字符串newMonth=“”;
对于(int i=2;i

我已经成功地设置了浏览器初始化和打开,现在如何在download_new_month方法中使用它,因为当条件满足时,必须单击3个xpath来下载报告。请帮助。

在下面使用的类的开头

  public static  WebDriver driver;
再次在publicstaticvoid中,使用

 WebDriver driver=new FirefoxDriver(fprofile);
此处看起来不正确,驱动程序已全局声明,因此请使用

 driver=new FirefoxDriver(fprofile);
在initiate_webdriver()方法中

谢谢,,
Murali

请发布正确的来源,并解释您遇到的问题这些是类文件,请参见此处的下载新方法