Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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
在selenium中从JavaScriptExecutor选择日历日期_Javascript_Eclipse_Selenium_Webdriver - Fatal编程技术网

在selenium中从JavaScriptExecutor选择日历日期

在selenium中从JavaScriptExecutor选择日历日期,javascript,eclipse,selenium,webdriver,Javascript,Eclipse,Selenium,Webdriver,我试图从selenium中的JavaScriptExecutor中选择日历日期,但该日期未被选择,控制台也没有给出任何错误。我无法理解这背后的原因。谁能帮忙吗。下面是我编写的selenium代码 包硒会话; 导入java.util.concurrent.TimeUnit; 导入org.openqa.selenium.By; 导入org.openqa.selenium.JavascriptExecutor; 导入org.openqa.selenium.WebDriver; 导入org.openqa

我试图从selenium中的JavaScriptExecutor中选择日历日期,但该日期未被选择,控制台也没有给出任何错误。我无法理解这背后的原因。谁能帮忙吗。下面是我编写的selenium代码

包硒会话;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.By;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类SelectCalendarByJS{
公共静态void main(字符串[]args){
System.setProperty(“webdriver.chrome.driver”,“F:\\Drivers\\chromedriver\U win32\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeout().pageLoadTimeout(40,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
驱动程序。获取(“https://www.makemytrip.com/");
//WebElement date=driver.findElement(By.xpath(“//div[@class='fsw\u inputBox dates inactiveWidget']”);
WebElement date=driver.findElement(By.xpath(//input[@id='execution']);
String dateVal=“2020年6月19日星期五”;
选择DateByJS(驱动程序、日期、日期值);
//driver.quit();
}
公共静态void selectDateByJS(WebDriver驱动程序、WebElement元素、字符串dateVal){
JavascriptExecutor js=((JavascriptExecutor)驱动程序);
js.executeScript(“参数[0].setAttribute('value','“+dateVal+”);”,元素);
}
}
下面是我选择的属性的HTMLDOM


makemytrip出发日期选择器元素的问题是您无法向其发送密钥。您的代码可以用于Expedia应用程序,在该应用程序中,您可以更改文本字段的value属性,这将起作用。但是在makemytrip的情况下,您必须单击元素,然后必须单击日期,如下面的代码所示。如果您将看到一个元素中发生了日期选择,而另一个元素中显示了所选日期(如果您将看到P标记,则您将意识到它将在P标记中显示所选日期,而不是在同一个元素中)


随时都可以,我的朋友。如果您遇到任何问题,请随时与我联系:)行“arguments[0]。click()”将单击单个元素,但如果我必须使用javascript executor单击多个按钮,该怎么办。我应该每次都在两行以下使用吗?JavascriptExecutor js=((JavascriptExecutor)驱动程序);js.executeScript(“参数[0].click();”,driver.findElement(By.xpath(//div[@class='DayPicker-Day'和@aria label='+dateVal+']));那么你有两个选择。可以为所有元素创建多个函数,也可以使用同一个函数,但需要再添加一个参数,以接受字符串格式@class='DayPicker-Day'的类属性值。它完全取决于应用程序的DOM。如果您面临任何问题,我们可以进一步讨论:)
  @Test
    public void test() throws InterruptedException {
         driver.manage().window().maximize();
         driver.manage().deleteAllCookies();

         driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         WebElement date = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]"));

         date.click();

        // System.out.println(driver.findElement(By.xpath()));

         Thread.sleep(3000);
         String dateVal = "Tue Jun 16 2020";
        selectDateByJS(driver, dateVal);

        Thread.sleep(3000);
    }

    public static void selectDateByJS(WebDriver driver, String dateVal) {

        JavascriptExecutor js = ((JavascriptExecutor)driver);
        js.executeScript("arguments[0].click();", driver.findElement(By.xpath("//div[@class='DayPicker-Day' and @aria-label='"+dateVal+ "']")));

    }